DATA Step, Macro, Functions and more

sas macro? or loop?

Reply
New Contributor
Posts: 3

sas macro? or loop?

I am new to SAS, i am trying to do something that would allow me to run this code , but with a different variable everytime. If put in different variables in the parenthesis then it sorts it and seperates out each of the variables, i want to have it sorted by variable and then by q2  and by q3.  How can i do this ?

 

proc sort;

by q2;

proc freq;

table (bmicat) *school;

by q2;

format q2 q2f.;

proc sort;

by q3;

proc freq;

table (bmicat)*school;

by q3;

format q3 q3f.;

proc freq;

table (bmicat)*school;

run;

Regular Contributor
Posts: 175

Re: sas macro? or loop?

Well, you can assign a macro variable like this:
%let variableName = whatever;
Later, you use it like so:
&variableName
or, if you want it in a character string:
"&variableName."
As your title suggests, you can use a loop:
%do i = 2 %to 3;
    proc sort;
        by q&i;
    proc freq;
        table (bmicat) *school;
        by q&i;
        format q&i q&i.f.;
%end;
proc freq;
    table (bmicat)*school;
run;
During the first iteration, i will be 2; during the second, it will be 3. SAS will replace &i with those values, respectively. The reason you might include a . after &i is because otherwise when you set the format, SAS thinks you want a macro variable called &if, but you have not defined it.
Super User
Posts: 19,878

Re: sas macro? or loop?

Where are your DATA= statements/portion of the code?

 

Is this operating on the same dataset? What's the end goal/rationale for doing this? 

 

I wonder if there's a better way to get what you need, it doesn't make a lot of sense to me to re-run the same procedure by different order except possibly for printing and manual review. 

New Contributor
Posts: 3

Re: sas macro? or loop?

This is the same data set.

 

The data portion has a lot of if/then where i create dichotomous variables out of multi-answer survey questions.

 

 

What the sort does, is it sorts it by gender and by grade, so i do need each variable sorted this way.


What i would like to do though is instead of deleting the variable and typing the next one, is to just enter all the variables at once and have the output be by variable where the variable is sorted by gender and grade.

 

so if i do

 

proc freq;

tables (variable1 variable2 variable3 variable4 ....)*school

run;

 

i would like the output to be each variable, sorted by corresponding gender variable and grade variable.

 

does this sort of make sense? i need this to enter the data in a report, it would just be easier than going back and forth between variables to see the gender and grade.

Super User
Posts: 5,518

Re: sas macro? or loop?

Here's an approach you might want to look at:

 

proc freq;

tables q2 * (bmicat) * school;

tables q3 * (bmicat) * school;

tables (bmicat) * school;

format q2 q2f. q3 q3f.;

run;

 

The percentages may come out a little differently, but if this is sufficient then you are home free with no sorting, no loops and no macros.

Super User
Posts: 11,343

Re: sas macro? or loop?

Posted in reply to Astounding

or

tables (q2 q3) * (bmicat * school);

 

Also depending on exactly what you are looking for a different procedure might work:

 

proc tabulate;

   class q2 q3 bmicat school;

   format q2 q2f. q3 q3f.;

   table q2, /*< one output table per formatted level*/

            bmicat,  /* one row per value/formatted level*/

            school * (n colpctn rowpctn); /* column headed by school with statistics under*/

   table q3,

            bmicat,

            school * (n colpctn rowpctn);

run;

Though tabulate treats missing somewhat differently than freq but useful and CLASS variables do not require sorting.

New Contributor
Posts: 3

Re: sas macro? or loop?

[ Edited ]
Posted in reply to Astounding

This does work but it gives me all the different variables that i would put instead of BMICAT by q2 (gender) and then all the different variables by q3

 

I want to be able to have the variable by q2 and by q3 but i want to keep that variable together.

 

so i want th eoutput to read

 

variable1 by gender

variable1 by grade

variable 1 overall freq

 

variable 2 by gender

variable 2 by grade

variable 2 overall freq

 

but right now when i do this it gives me output like this

variable 1 by gender

variable 2 by gender

 

variable 1 by grade

variable 2 by grade

 

variable 1 overall

variable 2 overall

 

 

 

Super User
Posts: 19,878

Re: sas macro? or loop?

That depends on how you write your table statement. It's hard to work without sample data, but does this look close:

 

data sample;
set sashelp.class;
array q(5) q1-q5;

do i=1 to 5;
q(i)=rand('bernoulli', 0.4);
end;

output;
run;

proc freq data=sample;
table (q1 q2 q3 q4 q5)*sex*age;
run;

 

 

Ask a Question
Discussion stats
  • 7 replies
  • 325 views
  • 0 likes
  • 5 in conversation