DATA Step, Macro, Functions and more

with macro and do loop to create multiple proc freq tables, why my sas code does not work

Accepted Solution Solved
Reply
Super Contributor
Posts: 345
Accepted Solution

with macro and do loop to create multiple proc freq tables, why my sas code does not work

data gray;
set gd;
daygra= re_bgn-joindate;
if daygra le 365 and daygra NE . then gra1y= 'Y';
else gra1y ='N';
 if daygra le 730 and daydep gt 365 then gra2y = 'Y';
else gra2y ='N';
 if daygra le 1096 and daygra gt 730 then gra3y = 'Y';
else dep3='N';
 if daydep le 1461 and daygra gt 1096  then gra4y = 'Y';
else gra3y ='N';
	run;
%macro demog (dem);

PROC freq DATA=gray;
TITLE "score  and &dem";
tables graiy*score &dem*score / norow nocol expected chisq;

RUN;
	%do i=1 %to 4;
%end;
%mend;

%demog(age);
%demog(sex);
%demog(edu);
run;
QUIT;

I want to see how many years people can graduate, whether it is related to their demographic features. any advice to fix it? Thanks.

 

Error message shows the variable, gra1y does not exist, that is not true, i did check the dataset. i also changed the variable name to gra1, gra2 but still it does not work. the error message showed gra1 does not exist


Accepted Solutions
Solution
‎06-22-2016 01:19 PM
Super User
Posts: 5,503

Re: with macro and do loop to create multiple proc freq tables, why my sas code does not work

There are a few issues to fix. 

 

First is the position of the %DO statement.  It should come before PROC FREQ.

 

Second is the use of grayiy.  It should be gray&i.y instead.

 

Finally, take a look at the prior DATA step.  I'm not sure that the ELSE statements are doing the right thing.

View solution in original post


All Replies
Super User
Posts: 19,789

Re: with macro and do loop to create multiple proc freq tables, why my sas code does not work

Reread the error and your code. 

 

You our have the letter i, not 1 in your code. 

Solution
‎06-22-2016 01:19 PM
Super User
Posts: 5,503

Re: with macro and do loop to create multiple proc freq tables, why my sas code does not work

There are a few issues to fix. 

 

First is the position of the %DO statement.  It should come before PROC FREQ.

 

Second is the use of grayiy.  It should be gray&i.y instead.

 

Finally, take a look at the prior DATA step.  I'm not sure that the ELSE statements are doing the right thing.

Super Contributor
Posts: 345

Re: with macro and do loop to create multiple proc freq tables, why my sas code does not work

Posted in reply to Astounding

Thank you so much!

Super User
Posts: 19,789

Re: with macro and do loop to create multiple proc freq tables, why my sas code does not work

If you use a different naming convention for your new variables you can avoid a macro. For example if you use gray+number instead you can use the colon to list all at once.

 

PROC freq DATA=gray;
TITLE "score  and &dem";
tables (gray:)*score &dem*score / norow nocol expected chisq;
run;
Super Contributor
Posts: 345

Re: with macro and do loop to create multiple proc freq tables, why my sas code does not work

mine is gray1y, gray2y

should I first change them to gray1, gray2, then I can use your code to avoid the do loop? Is that what you mean?

Thanks.

Super User
Posts: 19,789

Re: with macro and do loop to create multiple proc freq tables, why my sas code does not work

Try it and see. The beauty of coding is that you can try things and see if they work and if they don't you can change it.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 339 views
  • 1 like
  • 3 in conversation