BookmarkSubscribeRSS Feed
Smitha9
Fluorite | Level 6

Hi,

I have few macros which has a step includes same folder name and filename everytime I run;

example:

%content(folder1,filename1);

%sort(folder1,filename1);

%average(folder1,filename1);

---------------------------------------------------------------

%content(folder2,filename2);

%sort(folder2,filename2);

%average(folder2,filename2);

 

I dont want to type everytime folder1 and filename1 for each time. Is there a way that I could code something  if I want to change folder1 to folder2 for with one code?.

5 REPLIES 5
ballardw
Super User

It would likely help to show us what your folder values look like but something similar to this may be what you are looking for.

 

%let path= d:\data\subfolder;

%content(&path.,filename1);
%sort(&path.,filename1);
%average(&path.,filename1);

unless you mean to iterate over a bunch of folders.

mklangley
Lapis Lazuli | Level 10

Macro variables to the rescue!

Define what you would like folder and filename to be:

%let folder = folder1;
%let filename = filename1;

Then, use these macro variables as the parameters in your macro functions:

%content(&folder. , &filename.);   /* resolves to  %content(folder1, filename2)  */
%sort(&folder. , &filename.);    /* etc. */
%average(&folder. , &filename.);

 

Alternatively, if your folders and filenames always only differ by the number suffix, then you could do:

 

%let folderNbr = 1;
%let filenameNbr = 1;

%content(folder&folderNbr. , filename&filenameNbr.);  /* resolves to  %content(folder1, filename2)  */
%sort(folder&folderNbr. , filename&filenameNbr.);   /* etc. */
%average(folder&folderNbr. , filename&filenameNbr.);  

If you don't want to change the folder numbers manually for each run, you could even set up a loop to increment them automatically. 

 

 

 

Reeza
Super User
Make it dynamically data driven? Have a data set that has your file/folders and then use CALL EXECUTE() to pass those to the macro?
Reeza
Super User
data data2run;
infile cards dlm="|" dsd;
length folder $256. filename $256.;
input folder $ filename $;
cards;
C:\path to folder\demo | file1
C:\path to folder\sample | file23
;;;;
run;

data runReports;
set data2run;

str1 = catt('%content(', folder, ", ", filename, ");");
str2 = catt('%sort(', folder, ", ", filename, ");");
str3 = catt('%average(', folder, ", ", filename, ");");

call execute(str1);
call execute(str2);
call execute(str3);

run;

 

 

This is an example of what I would recommend. Add in your path and filenames to the first part of the code. The second part does not change and will execute all your macros for each line provided.

 

Kurt_Bremser
Super User

The absolute short version:

%macro run_all(folder,filename);
%content(&folder,&filename);
%sort(&folder,&filename);
%average(&folder,&filename);
%mend;

data _null_;
infile datalines dlm="|" dsd truncover;
input folder :$256. file :$256.;
call execute(cats('%nrstr(%run_all(',folder,',',file,'))'));
datalines;
folder1|filename1
folder2|filename2
;

Now you can call macro %run_all manually for a single shot, or use the data step for an arbitrary number of repeats.

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!
Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 5 replies
  • 574 views
  • 2 likes
  • 5 in conversation