BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ESpiel
Calcite | Level 5

I have many variables that I need to run through the same series of procedures--such as a proc freq, multiple proc phregs, proc means, and a proc univariate. In order to accomplish this, I have been using a macro variable, for example:

 

%let myvariable=grams_fruit

followed by my various procedures, such as 

 

proc freq data=mydata;
tables age*&myvariable / chisq;
where &myvariable ne 0;
run;

 

proc means data=mydata;
var age;
class &myvariable;

run;

and so on.

 

Then, I would run the let statement again with the next variable (%let myvariable=grams_veg), and run all of my procedures again.

 

Is there a better way to do this, where I can loop my list of variables to be used in this series of procedures? It is important that all of these procedures produce results for one variable (eg grams_fruit) before moving on to the next.

1 ACCEPTED SOLUTION

Accepted Solutions
ChrisNZ
Tourmaline | Level 20

Where is your variable list coming from?

To get you started:

 

%let myvariables = grams_fruit grams_vege;

%macro loop;
  %local myvariable i;
  %do i=1 %to %sysfunc(countw(&myvariables));
    %let myvariable =%scan(&myvariables,&i);

proc freq data=MYDATA;
  tables AGE*&myvariable / chisq;
  where &myvariable ne 0;
run; 

proc means data=MYDATA;
  var AGE;
  class &myvariable;
run;

  %end;
%mend;

Note that you can run some procedures with all variables in one step:

 

proc means data=MYDATA;
  var AGE;
  ways 1;
  class &myvariables;
run;

 

 

 

View solution in original post

6 REPLIES 6
ChrisNZ
Tourmaline | Level 20

Where is your variable list coming from?

To get you started:

 

%let myvariables = grams_fruit grams_vege;

%macro loop;
  %local myvariable i;
  %do i=1 %to %sysfunc(countw(&myvariables));
    %let myvariable =%scan(&myvariables,&i);

proc freq data=MYDATA;
  tables AGE*&myvariable / chisq;
  where &myvariable ne 0;
run; 

proc means data=MYDATA;
  var AGE;
  class &myvariable;
run;

  %end;
%mend;

Note that you can run some procedures with all variables in one step:

 

proc means data=MYDATA;
  var AGE;
  ways 1;
  class &myvariables;
run;

 

 

 

ESpiel
Calcite | Level 5

Editing this--it worked! I discovered my SAS was just jammed earlier and wasn't running anything. Thanks ChrisNZ.

 

Thanks for the response! Entering the list of variables with the %let statement would be perfect. I just tried this and I haven't been able to get it to run (trying with just one proc freq to test it). I actually don't have SAS on my local computer and remote in somewhere to use it, so excuse the screenshot over code. I am very new to macros--am I doing something wrong here? Do I need to do something else to call in the macro?

 

Screen Shot 2021-08-24 at 12.01.48 PM.png

 

PaigeMiller
Diamond | Level 26

 I just tried this and I haven't been able to get it to run (trying with just one proc freq to test it).


Tell us what happened and SHOW US the log. We need to see the entire log for this macro, every single line, in sequence, with nothing chopped out. Please copy the log as text (not a screen capture) and paste it into the window that appears when you click on the </> iconDO NOT SKIP THE PART IN RED.

 

Also as stated above by @ballardw and @ChrisNZ , you can do all of this without a macro. So I recommend strongly you do it without a macro. Make your life simple, don't use a macro when it is not necessary.

--
Paige Miller
ESpiel
Calcite | Level 5

As I mentioned in my reply, I do not have SAS on my local machine and am remoting in somewhere to use it. It is physically impossible to copy-paste from that environment, which is very locked-down. Thanks for your suggestion.

PaigeMiller
Diamond | Level 26

Then write your code without a macro, as shown above.

--
Paige Miller
ballardw
Super User

Since both of the procedures you show will handle many variables why do you want to do these one at a time?

It also might help to show what you want output to look like as one of the report procedures such as Report or Tabulate may be more flexible than means.

 

Personally what you showing in the proc freq code I would create helper data set, set all of the 0 to missing to the variables you are processing and then use the ( ) in the tables statement to create multiple output tables.

 

Proc freq data=sashelp.cars;
   tables origin *(make type cylinders) /chisq;
run;

will make tables of origin*make  origin*type and origin *cylinders.

 

Similar with Proc means will accept multiple variables on the class statement. Send the output to a data set. You would have a bunch of output but selecting the proper value of the automatic added variable _type_ would get summaries of each.

Or

Proc tabulate data=mydata;
   var age;
   class grams_fruit <other class variables>;
   table grams_fruit otherclassvar1 otherclassvar2 , 
          age *(mean min max std n)
   ;
run;

will have one table with each class variable level on a row and the statistics for age in columns.

Caveat with Tabulate, if your class variables have missing value you likely would want to include the /missing ; option on the class statement. Default behavior in tabulate is any record with a missing value for a class record is excluded.

The Tables statement would have all of the class variables.

 

or search the forum for Loop Macro list. We get a question on that almost daily and an example is in the documentation .

https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.4/mcrolref/n1qvxz5u3uru7yn1nk7q64ohvwak.htm

4A on that page shows how to extract one "word" from a list of values stored in a single macro variable.

Instead of the %put in that example is where you would place the code running for each variable (assuming it is in the list)

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 1975 views
  • 0 likes
  • 4 in conversation