DATA Step, Macro, Functions and more

Cycle Through Maco List?

Accepted Solution Solved
Reply
Contributor
Posts: 27
Accepted Solution

Cycle Through Maco List?

Is it possible for code to be ran by cycling through macros that I have listed?  For example, I am trying to run the exact same code three, on 3 different main datasets;  One dataset is all course types, one is face to face only course types, and the last is online only course types.  Right now I have three separate SAS files, one for each course type previously listed.  Is there a way that I can write a macro or program to run all the code for all course types, then run again for face to face, and then again for online?  I know that I can create macro variables and change the macro three times and have it work, but can I have it 'cycle' or 'run' through 3 macros that I have listed with changing it and running it 3 times, or running 3 separate files?

Reasoning for wanting this: Since the three files are exact with the exception of the main datasets used initially and the naming of created datasets (i.e. face to face have F2F_ as a prefix on all WORK files and internet have IT_), I am thinking that if I have an addition or edit I think have to edit 3 files, rather than just one.  Editing that many files can lead to more errors that may be avoidable.

Thanks in advance!


Accepted Solutions
Solution
‎05-29-2015 12:05 PM
Super User
Super User
Posts: 7,076

Re: Cycle Through Maco List?

Posted in reply to laura6728

BY processing suggestion.

Combine the data into one large dataset with a source variable.  Then add this source variable as a BY variable to all of your later steps.

data all ;

  length source indsname $41.;

   set f2f_input it_input indsname=indsname;

   source=indsname ;

run;

proc summary data=all ;

  by source ;

  output out=means;

run;

proc print data=means;

  by source;

run;

View solution in original post


All Replies
Trusted Advisor
Posts: 3,215

Re: Cycle Through Maco List?

Posted in reply to laura6728

That is why you can used macros (routine) or generate sas-code from a dataset (dosubl). Answer: Yes it is possible.

---->-- ja karman --<-----
Contributor
Posts: 27

Re: Cycle Through Maco List?

What should I do or search for on how to accomplish this?  Is there a specific macro I can use?

Super User
Super User
Posts: 7,076

Re: Cycle Through Maco List?

Posted in reply to laura6728

Yes. Or you could combine the datasets and process them all at once using BY statements.

One way to start is just replace the reference to the input data set with a macro variable instead.  Or perhaps with just the prefix part.

%let prefix=F2F_;

Let's assume your main analysis code that you want to repeat is something like this:

proc summary data=&prefix.source ;  output out=&prefix.means; run;

proc print data=&prefix.means; run;

You could then save the main code into a separate program and then call it three times.

%let prefix=F2F_;

%inc 'main_program.sas';

%let prefix=IT_ ;

%inc 'main_program.sas';

The next step is to convert your program to a SAS macro and then call it three times with different parameter value.

%macro main(prefix);

proc summary....

%mend main ;

%main(F2F_);

%main(IT_);

The next step is add a %DO loop so that you could call it with multiple prefixes at once and it would scan through them.  You could add this logic into the macro or make a new macro that does the looping and calls the other macro to do the work.

%macro main_looper(prefixlist);

%local i;

%do i=1 %to %sysfunc(countw(&prefixlist),%str( )) ;

  %main(%scan(&prefixlist,&i,%str( )));

%end;

%mend main_looper;

%main_looper(F2F_ IT_)

Contributor
Posts: 27

Re: Cycle Through Maco List?

Thank you, I am going to try this and see how it goes!

Super User
Posts: 19,869

Re: Cycle Through Maco List?

Posted in reply to laura6728

You can also use Call Execute and/or macro lists to add to your options.

I prefer call execute.

SAS - Call macro using parameters from data set

/********************************************************************

Example : Call macro using parameters from data set

********************************************************************/

proc sort data=sashelp.class out=class;

by age sex;

run;

%macro summary(age=, sex=);

proc print data=sashelp.class;

where age=&age and sex="&sex";

run;

%mend;

data sample;

set class;

by age sex;

if last.sex;

string =

  catt('%summary(age=', age, ',sex=', sex, ');');

put string;

run;

data _null_;

set sample;

call execute(string);

run;

Trusted Advisor
Posts: 3,215

Re: Cycle Through Maco List?

Posted in reply to laura6728

That is the next question. The dosubl function  calls code from a datastep the call execute is one that appends code to be executed after the running datastep. All 3 options are their advantages and disadvantages. The best one to choose is up to you and dependicy on the complextiy of your code and your skills. SAS(R) 9.4 Functions and CALL Routines: Reference, Third Edition

---->-- ja karman --<-----
Solution
‎05-29-2015 12:05 PM
Super User
Super User
Posts: 7,076

Re: Cycle Through Maco List?

Posted in reply to laura6728

BY processing suggestion.

Combine the data into one large dataset with a source variable.  Then add this source variable as a BY variable to all of your later steps.

data all ;

  length source indsname $41.;

   set f2f_input it_input indsname=indsname;

   source=indsname ;

run;

proc summary data=all ;

  by source ;

  output out=means;

run;

proc print data=means;

  by source;

run;

Contributor
Posts: 27

Re: Cycle Through Maco List?

This is what I ultimately used and worked, thank you!

Regular Contributor
Posts: 227

Re: Cycle Through Maco List?

Posted in reply to laura6728

I have written a macro, CallMacro,

that has two parameters

1. data set

2. macro name

I call the data set a list, SAS calls it a control data set;

see e.g. proc format cntlin =

Each row in the list is a set of parameters.

That means that the variables names must match the parameters of the macro being called.

DATA list_class_types;

attrib data length = $32;

data = 'all_course_types';

output;

data = 'face_to_face_only_course_types'

output;

data ='online_only_course_types';

output;

stop;

run;

%CallMacr(data = list_class_types,

macro_name = mymacro)

Ron Fehd  automagic maven

http://www.sascommunity.org/wiki/Macro_CallMacr

if you want to use it, ping me

as I have a few modifications that are not on the sco.wiki page.

🔒 This topic is solved and locked.

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

Discussion stats
  • 9 replies
  • 405 views
  • 7 likes
  • 5 in conversation