Hello,
i have about 30 datasets that i need to write a macro to get the proc contents from each dataset. i am new to the macro. i write the following but it doesn't work. can any expert take a look at my code:
%macro C1 (ennloop, list);
%do i = 1 %to &endloop.;
%let dem = %scan(&list., &i.);
proc contents data=&list;
run;
end;
%mend;
%C1(2, c2010 c2016 d2016 d2017 f2016 f2019);
1) What do you mean by 'Doesn't work''
2) Where does the list of data set names come from? Are they in a data set? If so, there are better ways.
Might be easier to get all the data sets in the library
Proc contents data=work._all_;
run;
would provide contents for all the data sets in the Work library.
Note in your code:
%macro C1 (ennloop, list); %do i = 1 %to &endloop.; %let dem = %scan(&list., &i.); proc contents data=&list; run; end; %mend;
You do not have an "endloop" parameter. Your macro statement defines "ennloop". Which should show one or more notes in the LOG (you do read the LOG don't your?)
No need to even pass such a count. You can use %do i = %sysfunc(countw(&list.)); and let the program count the number of values you pass in LIST so you don't have to.
NOTE that you example call to the macro
%C1(2, c2010 c2016 d2016 d2017 f2016 f2019);
if it "worked" , (no problem with the endloop/ennloop definitions) would only have output for the first two sets. IF they are in the WORK library.
When debugging macro code you generally start by setting OPTIONS MPRINT; before running the macro to get the code generated by the macro in the Log and read the log for the details.
@juliajulia wrote:
Hello,
i have about 30 datasets that i need to write a macro to get the proc contents from each dataset. i am new to the macro. i write the following but it doesn't work. can any expert take a look at my code:
%macro C1 (ennloop, list);
%do i = 1 %to &endloop.;
%let dem = %scan(&list., &i.);
proc contents data=&list;
run;
end;
%mend;
%C1(2, c2010 c2016 d2016 d2017 f2016 f2019);
You need to use DEM on your PROC CONTENTS statement:
%macro C1 (list);
%do i = %sysfunc(countw(&list.));
%let dem = %scan(&list., &i.);
proc contents data=&dem;
run;
end;
%mend;
%C1( c2010 c2016 d2016 d2017 f2016 f2019);
%macro C1(list);
%let ndsets = %sysfunc(countw(&list.));
%do i = 1 %to &ndsets;
%let dem = %scan(&list., &i.);
proc contents data=&dem;
run;
%end;
%mend;
%C1( c2010 c2016 d2016 d2017 f2016 f2019);
That should run.
Issues:
@juliajulia wrote:
Thank you for your response.
would you please explain more? i still can't make it work. i edited the code like this:
%macro C1 (list);
%do i = %sysfunc(countw(&list.));
%let dem = %scan(&list., &i.);
proc contents data=&list;
run;
end;
%mend;
%C1( c2010 c2016 d2016 d2017 f2016 f2019);
Check out the table sashelp.vtable and sashelp.vcolumn in your SAS installation.
It has the same metadata as proc contents and querying those tables is often easier than looping proc contents.
@juliajulia wrote:
there are about 50 files in the folder but i only want to proc contents for 20 of them. so the _all_ is not applicable.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.