全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 SAS专版
2200 1
2010-11-05
悬赏 50 个论坛币 未解决
前天我发帖悬赏了一个SAS macro的编写,有个牛人迅速编出,其他各路牛人纷纷赞同,学术奖章纷至沓来,我也设此为最佳答案,可是Q4,Q5这两个macro编写对于macro新手的我还是不太理解,能否请其他高手翻译一下这位高手的编程思路?

数据表如下:
复制代码
Q4. Complete the following macro.

%macroprocessyear(Inputds=StudentScore, Year=91,Outputds=Year_out);

%mend;

Year _out should have the following variables:
Year- Year specified in the input.
StudentHighScore- “StudentID “of the student with thehighest score for that year.
YearAvg- Average for the input year.

高手回复:
复制代码
Q5. Write a SAS macro to create individual student files foreach student/year combination.

高手回复:
复制代码
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

全部回复
2010-11-5 07:35:24
我来试试:

%macro processyear(Inputds,Year,Outputds);

   proc sql;
      create table &Outputds as          /*创建数据集,名为&outputds*/
        select                                        /*选择变量年份、编号、分数、平均分数*/
            Year
            ,StudentID
            ,Score
            ,Avg(Score) as YearAvg
        from &Inputds                         /*指定输入数据集&inputds*/
        where year=&year                 /*只选择年份为&year的数据*/
        group by   
                 Year                               /*按照年份分组*/
        order by
               Score desc                     /*将结果数据集按分数的降序排列*/
                ;
quit;

data &Outputds;
    set &Outputds;
    by descending Score;                                    /*按分数降序读入数据*/
    if _N_=1;                                                        /*选择第一条记录,即分数最高的记录*/
    rename StudentID=StudentHighScore;        /*更改变量名*/  
    drop Score;
run;

%mend processyear;

%processyear(StudentScore,95,Year_Out);  


%MACRO split;
    proc sort data=StudentScore;by StudentId year;run;quit;       /*将数据集按编号、年份排序*/
    data t1;
         set StudentScore;
         by StudentId year;                                  /*按编号、年份顺序依次读入数据*/
         Grp=compress(StudentID||'_'||year);     /*生成Grp变量,值为编号与年份组合,如A_91*/
         if first.year;                                             /*选择每个编号+年份组合的第一条记录,目的是对每个编号和年份组合只记录一次*/
         keep StudentID year Grp;                     /*保存需要的变量*/
    run;

    proc sql;
        select Grp into:output separated by '|' from t1;       /*将所有编号+年份的组合保存在宏变量output中,并以字符'|'分隔*/
    quit;

    %let cnt=%eval(%sysfunc(count(&output,|))+1);    /*数出编号+年份组合的个数,保存在宏变量cnt中*/

   %put &output &cnt;                                                    /*在log中输出宏变量output和cnt的值*/

    data
       %do i=1 %to &cnt;
         %scan(&output,&i,|)
       %end;
        ;                          /*在Data语句中列出所有的编号+年份组合。data A_91 A_92 B_91 ...;*/               
       set StudentScore;

    /*以下Do循环将每个编号和年份的数据输出到对应的数据集中。*/

       %do i=1 %to &cnt;
         %if &i>=2 %then %do;else%end;
         if StudentID="%scan(%scan(&output,&i,|),1,_)" and year=%scan(%scan(&output,&i,|),2,_) then output %scan(&output,&i,|);
      %end;
    ;
    run;
%MEND split;
%SPLIT;
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群