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

I am a new SAS user wondering the most efficient way to set up my program. It's made up of about a dozen DATA and PROC SQL steps that do basic transformations to my input data set. The final output is a simple table with summary metrics for that data set. I'd like to run several other data sets through the same script and have the program return summary tables for each one.  In R, I would approach this by creating a vector with all my data set names and then use a FOR LOOP to run the program for each of the data sets in the vector. Would this approach work in SAS and if so, what would the code look like? 

 

 

Thank you in advance for the help!

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
shubhayog
Obsidian | Level 7

Try Using SAS Macros, Ex. Considering you have multiple sorting operations on multiple datasets,

 

%macro srt(idsn=,odsn=,byvar=);
proc sort data=&isdn out=&odsn;
by=&byvar;
run;
%mend;

 

%srt(idsn=have,odsn=want,byvar=var1);

View solution in original post

3 REPLIES 3
shubhayog
Obsidian | Level 7

Try Using SAS Macros, Ex. Considering you have multiple sorting operations on multiple datasets,

 

%macro srt(idsn=,odsn=,byvar=);
proc sort data=&isdn out=&odsn;
by=&byvar;
run;
%mend;

 

%srt(idsn=have,odsn=want,byvar=var1);

Astounding
PROC Star

In SAS, the approach is somewhat similar.  Rather than creating a vector, create a long string holding all the input data set names (separated by a blank is fine).  Then use macro language to loop through the list, pick out the next name, and apply your series of steps to it.  The series of steps could be coded within your macro, or could just be packaged as a separate macro.  Here's a paper that might help with the approach:

 

http://blogs.sas.com/content/publishing/2015/01/30/sas-authors-tip-getting-the-macro-language-to-per...

 

Unfortunately, writing macros is a much longer subject.  Good luck.

ballardw
Super User

Another approach is to place your input dataset names (or library and set0 into a data set.

The values can then be used in a data step using that data to use CALL EXECUTE to create the syntax of the steps you execute.

 

A real crude example:

data names;
   length dsname $ 41;
   input dsname;
datalines;
SASHELP.Class
SASHELP.Cars
;
run;

data _null_;
   set names;
   call Execute("Proc print data="||dsname||";");
   call execute("run;");
run;

The data _null_ basically presents the statements from call execute to the compiler.

 

Any of your invariate code could be in a single Call Execute though I tend to make the lines one a time for later flexibility.

Replace Call Execute with appropriated put statements to see what the code generated looks like. You could use FILE PRINT to send the generated code to program file if desired instead.

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
  • 3 replies
  • 1574 views
  • 1 like
  • 4 in conversation