【问题标题】:sas coding: choosing max variablesas 编码:选择最大变量
【发布时间】:2014-06-25 06:33:46
【问题描述】:

我有两张表,需要再创建一张与其他两张一起使用的表:

  first_table:                      SECOND TABLE
id     term                      id           term      majr_code
3      2014                       3           2010     ACT
3      2015                       3           2010     ACT
4      2014                       3           2011     GNST
4      2015                       3           2015     BUSA
5      2013                       3           2015     BUSA
5      2014                       4           2009     TIM  
6      2013                       4           2010     BAL
6      2014                       4           2014     TAR
                                  5           2011     SAR
                                  5           2013    COR
                                  6           2010     PAT
                                  6           2013     TOR

这是我有的两张桌子。我需要创建另一个与第一个表相同的表并再添加一个列 majr_code。

    first_table:                      
id     term      majr_code               
3      2014       GNST              
3      2015       BUSA                
4      2014       TAR              
4      2015       TAR                
5      2013       COR
5      2014       COR          
6      2013       TOR              
6      2014       TOR

我需要做的是,对于相同的 id,如果第二个表与第一个表具有相同的术语,我将保留相同的 majr_code。例如:对于第一个表有 2014 年,第二个表有 2011 年和 2015 年,我需要使用 2011 年的 majr_Code 作为 2014 年任期。例如:第一个表有 2013 年和 2014 年相同 id 的词,如果第二个表的最高词是 2013,我将保留 2013 年和 2014 年相同的 majr_Code

我知道它很复杂,如果你检查表格和结果应该会更清楚。如果仍然复杂,我可以删除问题。我可以这样解释。谢谢!

【问题讨论】:

  • 我需要使用上表创建底部表

标签: sas


【解决方案1】:

我认为下面的代码应该可以解决问题。它的工作原理如下:

1) 读取样本数据集。

2) 创建一个名为 second_table_nogaps 的表,它只是 second_table,但到 2015 年没有年度差距。基本上,对于第二个表中的每个 ID,它会检查是否存在给定的年度记录。如果是,则输出记录,如果不是,则使用上一年的 majr_code 创建一条新记录。如果给定 id 的最后一条记录不是 2015 年,则会生成 2015 年之前的新记录。(例如,为 id=4、year=2014、majr_code = TAR 创建新记录)

3) 将id+term+majr_code的唯一值合并到first_table。结果表First_table_2 应该是您正在寻找的!但是,请注意,如果同一 id+term 有多个 majr_codes,则此步骤将导致重复。

希望这会有所帮助!步骤 2 中的代码可能会被简化,因为我对第一条和最后一条记录的处理不是特别有效。

  data first_table;
        infile datalines ;
        input id term;
        datalines ;
        3      2014 
        3      2015 
        4      2014 
        4      2015 
        5      2013 
        5      2014 
        6      2013 
        6      2014
        ;
    run;


data second_table;
    infile datalines ;
    input id term majr_code $;
    datalines ;
        3   2010    ACT
        3   2010    ACT
        3   2011    GNST
        3   2015    BUSA
        3   2015    BUSA
        4   2009    TIM
        4   2010    BAL
        4   2014    TAR
        5   2011    SAR
        5   2013    COR
        6   2010    PAT
        6   2013    TOR
    ;
run;

proc sort data=second_table ; by id term; run;

data second_table_nogaps (keep=id_nogaps term_nogaps majr_code_nogaps );
    set second_table end=eof;
    retain id_nogaps term_nogaps majr_code_nogaps ;

    *first set up the first row... establishes retained variables and outputs;
    if _N_ = 1 then do;
                id_nogaps = id ; 
                term_nogaps = term;
                majr_code_nogaps = majr_code;
                output;
        end;

        *for all but the first and last row;
        else if not eof then do;
            do while (  (term_nogaps + 1 < term ) /*this is to fill in gaps between years. (e.g. major code in 2011 and major code in 2014 within the same id*/
                        or 
                        ((id_nogaps ne id) and term_nogaps < 2015) /*this is to fill major code for all terms up through 2015 (e.g. last major code for id 4 is in 2014)*/
                      );
                term_nogaps = term_nogaps + 1;
                output;
            end;

            id_nogaps=id;
            term_nogaps = term;
            majr_code_nogaps=majr_code;
            output;
        end;

        else do;
            do while (term_nogaps + 1 < term );
                term_nogaps = term_nogaps + 1;
                output;
            end;
            id_nogaps=id;
            term_nogaps = term;
            majr_code_nogaps=majr_code;
            output;
            do while ( term_nogaps < 2015 );
                term_nogaps = term_nogaps + 1;
                output;
            end;
    end;
run;

proc sql;
    create table First_table_2 as 
    Select a.* , b.majr_code_nogaps as majr_code
    from first_table a
        left join 
            (select distinct id_nogaps, term_nogaps, majr_code_nogaps from second_table_nogaps) b /*select distinct values to prevent duplication*/
    on a.id   =   b.id_nogaps  and a.term = b.term_nogaps;
quit;

【讨论】:

    【解决方案2】:

    有几种方法可以解决这个问题,但sql 可能是最简单的。你不提供代码,所以我只包含一个指针。分组到having term=max(term) 后,您需要使用having 过滤表。

    【讨论】:

      猜你喜欢
      • 2015-09-16
      • 1970-01-01
      • 2014-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多