基于ID的SAS创建动态表,仅显示前3名

时间:2013-10-23 18:48:09

标签: sas

您好我真的想根据以下示例数据创建动态表,根据PAYEE_ID创建4个新数据集:522,622,743和888.我希望所有字段都在新的4个数据集中,但是对于每种类型的PAYEE_ID

,在4个表中只有前3个AMT_BILLED
PAYEE_ID    PAYEENAME   MSG_CODE    MSG_DESCRIPTION AMT_BILLED  percentbilled   claimscounts    PercentLines    TotalAmount TotNumofClaims
522 MakeBelieve Center 1    AA  text field 1    10000   4%  50  16% 275000  305
522 MakeBelieve Center 1    BB  text field 2    20000   7%  40  13% 275000  305
522 MakeBelieve Center 1    6N  text field 3    30000   11% 30  10% 275000  305
522 MakeBelieve Center 1    5U  text field 4    25000   9%  20  7%  275000  305
522 MakeBelieve Center 1    1F  text field 5    90000   33% 100 33% 275000  305
522 MakeBelieve Center 1    2E  text field 6    100000  36% 65  21% 275000  305
622 Invisible Center 2  A4  text field 1    600 2%  9   7%  34300   134
622 Invisible Center 2  D2  text field 2    700 2%  31  23% 34300   134
622 Invisible Center 2  D4  text field 3    8000    23% 11  8%  34300   134
622 Invisible Center 2  DS  text field 4    10000   29% 62  46% 34300   134
622 Invisible Center 2  F8  text field 5    15000   44% 21  16% 34300   134
743 Pretend Center 1    1K  text field 1    440 1%  2   1%  41040   246
743 Pretend Center 1    1N  text field 2    3000    7%  7   3%  41040   246
743 Pretend Center 1    1V  text field 3    400 1%  4   2%  41040   246
743 Pretend Center 1    2W  text field 4    15000   37% 63  26% 41040   246
743 Pretend Center 1    3B  text field 5    500 1%  2   1%  41040   246
743 Pretend Center 1    3H  text field 6    7700    19% 41  17% 41040   246
743 Pretend Center 1    3Z  text field 7    14000   34% 127 52% 41040   246
888 It's A MakeBelieve One  B7  text field 1    68000   38% 257 29% 178449  886
888 It's A MakeBelieve One  B8  text field 2    5000    3%  47  5%  178449  886
888 It's A MakeBelieve One  B9  text field 3    200 0%  138 16% 178449  886
888 It's A MakeBelieve One  BB  text field 4    1562    1%  18  2%  178449  886
888 It's A MakeBelieve One  BO  text field 5    39999   22% 3   0%  178449  886
888 It's A MakeBelieve One  BZ  text field 6    40000   22% 2   0%  178449  886
888 It's A MakeBelieve One  C2  text field 7    500 0%  5   1%  178449  886
888 It's A MakeBelieve One  C5  text field 8    7865    4%  395 45% 178449  886
888 It's A MakeBelieve One  C7  text field 9    8649    5%  14  2%  178449  886
888 It's A MakeBelieve One  CR  text field 10   5674    3%  1   0%  178449  886
888 It's A MakeBelieve One  CX  text field 11   1000    1%  6   1%  178449  886

我是SAS的新手,这对我有帮助。非常感谢你!

1 个答案:

答案 0 :(得分:0)

proc sort data=sampleData out=sampleData_s;
    by payee_id amt_billed;
run;

如果“顶部”表示最大值,则可以使用降序,例如by payee_id descending amt_billed;

对数据进行排序后,您就可以读入数据步骤并使用firstlast,例如

data partial_solution(drop=count);
    retain count 0;
    set sampleData_s;
    by payee_id descending amt_billed;
    if first.payee_id then count=0;
    count+1;
    if count le 3 then output;
run;

输出到不同的数据集名称:

proc sort data=sampleData(keep=payee_id) out=all_payee_ids nodupkey;
    by payee_id;
run;

data _null_;
    length id_list $10000;   * needs to be long enough to contain all ids;
                             * if you do not state this, sas will default; 
                             * length to first value;
    retain id_list;
    set all_payee_ids end=eof;
    id_list = catx('|', id_list, payee_id);
    if eof then call symputx('macroVarIdList', id_list);
run;

你现在有一个管道分隔的所有你的id列表。您可以使用它们循环浏览这些数据集以创建数据集的名称。您需要这样做,因为SAS需要知道您想要输出的数据集的名称,例如

data ds1 ds2 ds3 ds4;
    set some_guff;
    if blah then output ds1;
    else if blahblah then output ds2;
    else output d3;
    output d4;
run;

使用宏var循环:

%let nrVars=%sysfunc(countw(&macroVarIdList));

data
    %do i = 1 %to &nrVars;
        dataset_%scan(&macroVarIdList,&i,|)
    %end;
    ;
    set partial_solution;
    count+1;
    %do j = 1 %to &nrVars;
        %let thisPayeeId=%scan(&macroVarIdList,&j,|);
        if payee_id = "&thisPayeeId" then output dataset_&thisPayeeId.;
    %end;
run;