BookmarkSubscribeRSS Feed
juliajulia
Obsidian | Level 7

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);

7 REPLIES 7
PeterClemmensen
Tourmaline | Level 20

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.

ballardw
Super User

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);


 

juliajulia
Obsidian | Level 7
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);
SASKiwi
PROC Star

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);
Reeza
Super User
%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:

 

  • %DO loop syntax is incorrect for a loop (just one value) 
  • No %END for ending macro loop
  • Reference &list instead of the &dem variable

 


@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);

 

juliajulia
Obsidian | Level 7
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.
Reeza
Super User

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.

 

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
  • 7 replies
  • 481 views
  • 1 like
  • 5 in conversation