楼主: lovedieer
2181 14

[问答] 请帮忙改写一个SAS程序 [推广有奖]

  • 0关注
  • 1粉丝

硕士生

60%

还不是VIP/贵宾

-

威望
0
论坛币
11 个
通用积分
2.0724
学术水平
1 点
热心指数
1 点
信用等级
1 点
经验
17239 点
帖子
146
精华
0
在线时间
201 小时
注册时间
2009-4-22
最后登录
2022-9-12

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
我现在有一个SAS程序,一共有20个问题(Q1-Q20), 每个问题的基本程序都一样,只有两处不同,第一就是每个问题都有一个不同的TITLE, 譬如说 对于Q1: TITLE"Q1:WHAT IS YOUR NAME", 对于Q2 :TITLE "Q2:WHAT IS YOUR DOB"等等。第二个不同就是把程序里的Q1变成Q2,Q3。。。。。

我只会把同样的程序复制20遍,然后改一下里面的TITLE内容并且把Q1什么的带换一下。如果需要修改某部分程序,要修改20次才行,
  1. Title5 h=10pt "Q1)  Process: What is your Name";
  2. proc sql;
  3. create table loc_Q1 as
  4. select distinct upcase(Target)as Target, Region, reviewtype,
  5. sum(Q1='Yes' or Q1='No') as total , sum(Q1='Yes') as yes_count, calculated yes_count / calculated total as per format=percent9.2
  6. from sasuser.Demo
  7. where Target Contains "&Target" and input("&FromDate.",mmddyy10.)<= (sigDate)<=input ("&todate.",mmddyy10.)
  8. group by Region, reviewtype; quit;

  9. proc report nowd data=loc_Q1;
  10. column Region region_name reviewtype,(yes_count total Percentage)
  11. ("Region Total" Region_yes Region_all Region_per);
  12. define Region /group 'Region' noprint;
  13. define region_name /computed 'Region';
  14. define reviewtype / 'Review Type' across;
  15. define yes_count/analysis 'Yes Number';
  16. define total/analysis 'Total Number';
  17. define percentage/computed "Percentage" format=percent8.2;
  18. define Region_yes/computed "YES Number"   ;
  19. define Region_all/"Total Number" Computed;
  20. define region_per/computed "Percentage" format=percent8.2;

  21.    compute region_name /char; region_name=Region;if region_name='' then region_name='Total';endcomp;
  22.    compute percentage;   _c5_ = _c3_/_c4_;   _c8_= _c6_/_c7_;   endcomp;
  23.    compute Region_yes ;Region_yes= sum(_c3_ ,_c6_);endcomp;
  24.    compute Region_all;Region_all=sum(_c4_,_c7_);endcomp;
  25.    compute Region_per;region_per=_c9_/_c10_;endcomp;
  26. rbreak after /summarize ;
  27. run;
复制代码

请高手帮我精简一下程序。
二维码

扫码加我 拉你入群

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

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

关键词:sas程序 calculate proc sql Distinct sasuser 程序

沙发
yongyitian 发表于 2013-3-12 08:41:24 |只看作者 |坛友微信交流群
This is a good example using macro and macro variables. Try the follows

/* 1.
  Replace the number in TITLE5 with a macro reference &num, so the title5 becomes title&num
  Replace the Q1 in your code with a macro reference &QQ,
  Replace the text in the title with a macro reference &titletext, so the first line of you code become
Title&num h=10pt “&QQ)   &titletext”;
*/

/* 2. Change your code to a macro named mymacro, then compile the macro */
%macro mymacro;

Your code here

%mend mymacro;


/* 3. Assign the values to the macro variables, then submit the macro as */
%let num=1;
%let QQ=Q1;
%let titletext= Process: What is your Name;
%mymacro

/* 4. Repeat the step 3 when you change the values of the macro variables */

使用道具

藤椅
lovedieer 发表于 2013-3-12 20:35:33 |只看作者 |坛友微信交流群
Thanks very much.
with this macro, I change the num, QQ, and titletext, the program will run one time which mean only one question processed.
If you do not mind, can I ask whether there's a way i can run all these 20 questions in one time.

使用道具

板凳
yongyitian 发表于 2013-3-12 21:56:52 |只看作者 |坛友微信交流群
lovedieer 发表于 2013-3-12 20:35
Thanks very much.
with this macro, I change the num, QQ, and titletext, the program will run one t ...
try this

data A;
input num qq $2.  @6 titletext  $50.;
datalines;
1 Q1 Process: What is your Name
2 Q2 Process: what is your city
3 Q3 Process: what is your address
;run;

%macro mymacro;
     title&num h=10pct "&QQ   &titletext";
      proc print data=a;
          run;
%mend mymacro;

data _null_;
     set a;
         call symput('num', num);
         call symput('QQ', QQ);
         call symput('titletext', titletext);
         call execute('%mymacro');
run;
已有 1 人评分学术水平 热心指数 信用等级 收起 理由
lovedieer + 1 + 1 + 1 热心帮助其他会员

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

使用道具

报纸
lovedieer 发表于 2013-3-12 22:21:41 |只看作者 |坛友微信交流群
Thank you very much. I am learning macro now. just finish reading the contents and need practice.

使用道具

地板
Eternal0601 发表于 2013-3-12 22:31:27 |只看作者 |坛友微信交流群
yongyitian 发表于 2013-3-12 21:56
try this

data A;
Great. And I wonder if there is any method to eliminate the warning about the title statement in the log window. Thanks.

使用道具

7
yongyitian 发表于 2013-3-12 22:51:49 |只看作者 |坛友微信交流群
Eternal0601 发表于 2013-3-12 22:31
Great. And I wonder if there is any method to eliminate the warning about the title statement in t ...
I run the code again, did not see any warning message. But the title is not shown in the html output window. I don't know why. What is warning you got?

使用道具

8
Eternal0601 发表于 2013-3-12 22:57:17 |只看作者 |坛友微信交流群
yongyitian 发表于 2013-3-12 22:51
I run the code again, did not see any warning message. But the title is not shown in the html outp ...
NOTE: CALL EXECUTE generated line.
1   + title           1 h=10pct "Q1   Process: What is your Name                        ";
WARNING: The TITLE statement is ambiguous due to invalid options or unquoted text.
1   +                                                                                            proc
print data=a;           run;

NOTE: There were 3 observations read from the data set WORK.A.
NOTE: PROCEDURE PRINT used (Total process time):
      real time           0.32 seconds
      cpu time            0.03 seconds


2   + title           2 h=10pct "Q2   Process: what is your city                        ";
WARNING: The TITLE statement is ambiguous due to invalid options or unquoted text.
2   +                                                                                            proc
print data=a;           run;

NOTE: There were 3 observations read from the data set WORK.A.
NOTE: PROCEDURE PRINT used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds


3   + title           3 h=10pct "Q3   Process: what is your address                     ";
WARNING: The TITLE statement is ambiguous due to invalid options or unquoted text.
3   +                                                                                            proc
print data=a;           run;

NOTE: There were 3 observations read from the data set WORK.A.
NOTE: PROCEDURE PRINT used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds



使用道具

9
Eternal0601 发表于 2013-3-12 23:25:02 |只看作者 |坛友微信交流群
yongyitian 发表于 2013-3-12 22:51
I run the code again, did not see any warning message. But the title is not shown in the html outp ...
I got it. &num in the title statement will be returned as '1'  '2'  '3', which cause the warning in the log window. After removing &num in the statement, there is no such error.

http://listserv.uga.edu/cgi-bin/ ... ;L=sas-l&P=2440

使用道具

10
lovedieer 发表于 2013-3-13 00:57:55 |只看作者 |坛友微信交流群
I did several modification with your code to fit my program. and it works great. Thank you very much.

使用道具

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

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

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

GMT+8, 2024-5-1 08:11