全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 SAS专版
2592 14
2013-03-11
我现在有一个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次才行,
复制代码

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

扫码加我 拉你入群

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

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

全部回复
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 */
二维码

扫码加我 拉你入群

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

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

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.
二维码

扫码加我 拉你入群

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

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

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;
二维码

扫码加我 拉你入群

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

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

2013-3-12 22:21:41
Thank you very much. I am learning macro now. just finish reading the contents and need practice.
二维码

扫码加我 拉你入群

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

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

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.
二维码

扫码加我 拉你入群

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

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

点击查看更多内容…
相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

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