楼主: yellowriver
1982 6

[原创博文] SAS新手竭诚求教代码编写(2) [推广有奖]

  • 0关注
  • 4粉丝

VIP

博士生

38%

还不是VIP/贵宾

-

威望
0
论坛币
10028 个
通用积分
22.7632
学术水平
21 点
热心指数
26 点
信用等级
19 点
经验
3133 点
帖子
164
精华
0
在线时间
170 小时
注册时间
2007-11-4
最后登录
2019-5-18

100论坛币
续前一个帖子“SAS新手竭诚求教代码编写(1)" http://www.pinggu.org/bbs/thread-950347-1-1.html, 另外两个问题还是和macro有关,100论坛币悬赏解决:Q4. 完成以下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.




谢谢大家!
关键词:combination Individual specified individua Variables individual following specified create should

本帖被以下文库推荐

沙发
soporaeternus 发表于 2010-11-2 14:30:24 |只看作者 |坛友微信交流群
  1. /*Q4*/
  2. %macro processyear(Inputds,Year,Outputds);
  3.         proc sql;
  4.                 create table &Outputds as
  5.                         select
  6.                                 Year
  7.                                 ,StudentID
  8.                                 ,Score
  9.                                 ,Avg(Score) as YearAvg
  10.                         from &Inputds
  11.                         where year=&year
  12.                         group by
  13.                                 Year
  14.                         order by
  15.                                 Score desc
  16.                 ;
  17.         quit;
  18.         data &Outputds;
  19.                 set &Outputds;
  20.                 by descending Score;
  21.                 if _N_=1;
  22.                 rename StudentID=StudentHighScore;
  23.                 drop Score;
  24.         run;
  25. %mend processyear;
  26. %processyear(StudentScore,95,Year_Out);

  27. /*Q5*/
  28. %MACRO split;
  29.         proc sort data=StudentScore;by StudentId year;run;quit;
  30.         data t1;
  31.                 set StudentScore;
  32.                 by StudentId year;
  33.                 Grp=compress(StudentID||'_'||year);
  34.                 if first.year;
  35.                 keep StudentID year Grp;
  36.         run;
  37.         proc sql;
  38.                 select Grp into:output separated by '|' from t1;
  39.         quit;
  40.         %let cnt=%eval(%sysfunc(count(&output,|))+1);
  41.         %put &output &cnt;
  42.        
  43.         data
  44.                 %do i=1 %to &cnt;
  45.                         %scan(&output,&i,|)
  46.                 %end;
  47.         ;
  48.                 set StudentScore;
  49.                 %do i=1 %to &cnt;
  50.                         %if &i>=2 %then %do;else%end;
  51.                         if StudentID="%scan(%scan(&output,&i,|),1,_)" and year=%scan(%scan(&output,&i,|),2,_) then output %scan(&output,&i,|);
  52.                 %end;

  53.         ;
  54.         run;

  55. %MEND split;
  56. %SPLIT;
复制代码
已有 3 人评分学术水平 热心指数 信用等级 收起 理由
peijiamei + 4 + 3 精彩帖子
hopewell + 1 + 1 + 1 我很赞同
pobel + 1 + 1 + 1 精彩帖子

总评分: 学术水平 + 6  热心指数 + 5  信用等级 + 2   查看全部评分

Let them be hard, but never unjust

使用道具

藤椅
tingdiren 学生认证  发表于 2010-11-2 14:39:22 |只看作者 |坛友微信交流群
不懂啊,呵呵

使用道具

板凳
huominbo888 发表于 2010-11-3 13:16:56 |只看作者 |坛友微信交流群
3# soporaeternus
请问这个代码是如何嵌到网页上的?

使用道具

报纸
pobel 在职认证  发表于 2010-11-3 13:45:35 |只看作者 |坛友微信交流群
  1. 点回复窗口上的"<>"就可以了(在笑脸的左边)
复制代码
和谐拯救危机

使用道具

地板
yellowriver 发表于 2010-11-5 04:25:51 |只看作者 |坛友微信交流群
高手们能否给我解释一下这位高手的code意思?不是特别理解。谢谢!

2# soporaeternus

使用道具

7
yellowriver 发表于 2010-11-5 04:34:00 |只看作者 |坛友微信交流群

请高手解释高手编的code~

前天我发帖悬赏了一个SAS macro的编写,有个牛人迅速编出,其他各路牛人纷纷赞同,学术奖章纷至沓来,我也设此为最佳答案,可是Q4,Q5这两个macro编写对于macro新手的我还是不太理解,能否请其他高手翻译一下这位高手的编程思路? 我的原帖问题详见如下引用部分:
yellowriver 发表于 2010-11-2 14:30
续前一个帖子“SAS新手竭诚求教代码编写(1)" http://www.pinggu.org/bbs/thread-950347-1-1.html, 另外两个问题还是和macro有关,100论坛币悬赏解决:
Q4. 完成以下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.




谢谢大家!

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-6-19 09:37