DATA Step, Macro, Functions and more

Macro to Loop thru directories

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 126
Accepted Solution

Macro to Loop thru directories

I'm trying to write a macro to loop thru several similarly named directories and run a Proc Contents on the file in that directory and I am certainly no macro maven. The directories are named C:\temp\SAS\FY89 to C:\temp\SAS\FY00 where FY is fiscal year. Each directory contains one SAS data file named FYXX where XX is the last two digits of the year. Here's what I have so far but it immediately chokes on the newvar=%put(fy, 3.);  line. What I'm trying to do there is convert the numeric variable fy to charaacter variable newvar. Anyone know why that doesn't work? The error message in the log syas

WARNING: Apparent symbolic reference NEWVAR not resolved

 

 

 

%Macro contents;

%do i=1 %to 11;

%Let fy =i + 88;

newvar=%put(fy, 3.);

%if %length(newvar) > 2 %then newvar = %substr(newvar,2,2);

%Let fpath = /mdr_pub_misc/sidr/fy&newvar;

libname sidr "&fpath";

Title "Contents of SIDR FY&newvar";

proc contents data=sidr.fy&newvar; run;

%end;

%let dir1=%str(C:\temp\SAS);

%let pgmname=%str(sidr_16);

%let date=_&sysdate;

%let time=_&systime;

%let ext1=%str(log);

%let break=%str(-);

%let final1=&dir1&pgmname&date&time..&ext1;

%put &final1;

proc printto log="&final1";

run; quit;

%Mend;

%contents;

 

Thanks,

Brian

 


Accepted Solutions
Solution
‎07-20-2016 03:49 PM
Super User
Posts: 17,912

Re: Macro to Loop thru directories

Something like this would work. 

 

You may want to write a second datastep to deassign the librefs after. 

 

data libname;
do i=1989 to 2000;
directory = catt("C:\temp\sas\FY", substr(put(i, 4.), 3, 2));
libref = "FY"||substr(put(i, 4.), 3, 2);
rc = libname(libref, directory);
output;
end;
run;

proc sql;
create table interest as
select *
from sashelp.vcolumn
where libname like 'FY%';
quit;

View solution in original post


All Replies
Super User
Posts: 17,912

Re: Macro to Loop thru directories

Your second let is incorrect. You don't reference the macro variable I, missing the &,and if your doing math you need %eval(). You maybe better off using a data step to create your macro variables or use the intnx function. 

 

And your mixing data step and macro language. 

 

How many paths are you referencing? If only the 11, consider a single libname statement followed by a proc datasets call. 

 

Or use a data step with a libname function to assign the libnames and then query sashelp table for the info your interested in. So what information from proc contents are you interested in?

Super User
Posts: 10,538

Re: Macro to Loop thru directories

To expand on @Reeza's response, you can have multiple directories in a single libname statement

 

libname sidr ("/mdr_pub_misc/sidr/fyVAR1" "/mdr_pub_misc/sidr/fyVar2" "/mdr_pub_misc/sidr/fyVar3");

 

Examine the properties of your SASHELP library. It could reference as many as a dozen folders depending on the number of SAS modules you have licensed.

Solution
‎07-20-2016 03:49 PM
Super User
Posts: 17,912

Re: Macro to Loop thru directories

Something like this would work. 

 

You may want to write a second datastep to deassign the librefs after. 

 

data libname;
do i=1989 to 2000;
directory = catt("C:\temp\sas\FY", substr(put(i, 4.), 3, 2));
libref = "FY"||substr(put(i, 4.), 3, 2);
rc = libname(libref, directory);
output;
end;
run;

proc sql;
create table interest as
select *
from sashelp.vcolumn
where libname like 'FY%';
quit;
Frequent Contributor
Posts: 126

Re: Macro to Loop thru directories

Reeza,

Sorry for the delayed response. Thanks for this post, it is very helpful!
☑ This topic is solved.

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

Discussion stats
  • 4 replies
  • 251 views
  • 3 likes
  • 3 in conversation