09-25-2011 12:57 PM
I am performing a macro with statements :
%macro stuff ();
%do i=2 %to 6 ....... followed by series of statements and then macro end statement.
My question is when I write "%do i=2 %to 6", this mean that macro would perform on colum 2 to column 6 in my data set.
But if I want to skip column 4 and start macro function again from 5, then which statement should I write for "%do i=2 %to 6".
I have tried several different statements like
%do i=2 %to 3 and %do i=5 %to 6 but nothing is working.
Is there a way to do this in same statement or should I have to write it in altogether different syntax?
Thanks a lot for your suggestions.
09-25-2011 01:13 PM
I'm not sure if I understand what you are trying to do. If I do understand you correctly, couldn't you just nest a %if statement in your code to skip the undesired column? E.g.:
1 1 1 1 1 1
2 2 2 2 2 2
1 2 3 4 5 6
%do i=1 %to 6;
%if &i. ne 4 %then %do;
09-25-2011 05:40 PM
Thanks for your reply.
actually what I want is:
I have Questions 1 to 200 in my dataset as column.
Role Q1 Q2 Q3 Q4 Q5.......Q200
I have to perform a same macro function for Q5 to Q12 and Q100 to Q110. so in
%do i=5 to %to 12 statement, woould ie be possible for me to include Q100 to Q110 as well excluding all questions (i.e Q 13 toQ99) in between.
I am triying the shorten the code by avoiding wrting macro %do stratements again for Q100 to Q110 as I did for Q5 to Q12.
Is that possible?
09-25-2011 05:44 PM
I'd still recommend nesting a %if statement. e.g.,
%do i=5 %to 110;
%if &i. le 12 or &i. ge 100 %then %do;
09-25-2011 05:52 PM
Are you sure you want a macro? If you are performing these operations in a datastep then just make an array and loop of the array.
For example to recode 99 to missing for a select group of variables.
array m99 q5-q12 q100-q112 ;
do i=1 to dim(m99);
if m99(i)=99 then varlist(i)=.;
Now you cannot do the exact thing in macro language but you can get close by putting you list of variable names in one macro variable and assigning the variable name to another macro variable using a loop counter and the scan function.
%let varlist = q5 q6 q7 q8 q9 q10 q11 q12 q100 q101 q102 q103 q104 q105 q106 q107 q108 q109 q110;
%do i=1 %to %sysfunc(countw(&varlist));
. . .
09-25-2011 06:50 PM
I tried this for different questions and my code looks likethis:
%let varlist = Q4 Q6 Q7 Q10 Q13;
%do i=4 %to %sysfunc(countw(&varlist.));
proc sql noprint;
create table test&i. as
select role, cats(count(Q&i.),'(',put(count(Q&i.)/count(*),percent8.),')') as C&i.
group by role;
Its giving me only 4 test data, test 4 test 5 test 6 and test 7. Also, I want my test data to br named as my selected questions. for eg: with above example the test data should be test 4, test 6, test 7, test 10 and test 13.
09-25-2011 07:28 PM
Looks like you want to loop over a set of SUFFIXES and use those suffixes for both specifing the name of the output dataset and the variable name.
%local i j ;
proc sql noprint;
%do i=1 %to %sysfunc(countw(&suffixes));
create table test&j as
, cats(count(Q&j),'(',put(count(Q&j)/count(*),percent8.),')') as C&j
group by role
%stuff(4 6 7 10 13);
09-25-2011 11:54 PM
Another way is to rename your variable.
proc datasets library=work nolist;
rename Q50-Q60=A1-A10 Q100-Q120=A11-A31;
Then use your loop macro code.
The macro code looks like my code?
Message was edited by: xia keshan