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
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;
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?
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.
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;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.