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

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!

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

9 REPLIES 9
jakarman
Barite | Level 11

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

---->-- ja karman --<-----
laura6728
Obsidian | Level 7

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

Tom
Super User Tom
Super User

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_)

laura6728
Obsidian | Level 7

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

Reeza
Super User

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;

jakarman
Barite | Level 11

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 --<-----
Tom
Super User Tom
Super User

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;

laura6728
Obsidian | Level 7

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

Ron_MacroMaven
Lapis Lazuli | Level 10

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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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
  • 9 replies
  • 1336 views
  • 7 likes
  • 5 in conversation