BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
sathya66
Barite | Level 11

Hi All,

I have 100+ sas datasets in each library that contain data on consumers individual characteristics such as id, gender, age,etc.

How can I write a multiple loop to go through each of the datasets in each library, do some manipulations and save the estimated values to a same file.

 

 my code;

data test.table;
	set test.table
	%testall(test.table);

run;

 

 I want to apply %testall macro  for  all the tables in all libraries.

 

Thanks,

SS

1 ACCEPTED SOLUTION

Accepted Solutions
sathya66
Barite | Level 11

Thanks  Miller,

I have different approach , please can you review .

%macro loopOverDatasets(inLibref);  
    ods output Members=Members;
 proc datasets library=&inLibref memtype=data;
 run;
 quit;
    %local datasetCount iter inLibref inMember;
    /*get number of datasets*/
    proc sql noprint;
        select count(*)
         into :datasetCount
        from WORK.Members;
    quit;

    /*initiate loop*/
    %let iter=1;
    %do %while (&iter.<= &datasetCount.);
        
        data _NULL_;
            set WORK.Members (firstobs=&iter. obs=&iter.); *only read 1 record;
         
            call symput("inMember",strip(Name));
           
        run;

        /* applying my logic to the dataset*/
        data &inLibref..&inMember.; 
            set &inLibref..&inMember.;
          %testall(&inLibref..&inMember.);
        run;



        /*increment the iterator of the loop*/
        %let iter=%eval(&iter.+1);
    %end;
%mend;

/*call the macro*/
%loopOverDatasets(work);

Thanks,

 

SS

View solution in original post

5 REPLIES 5
PaigeMiller
Diamond | Level 26

You need to create a macro variable that has the name of the 100 tables (probably using PROC SQL), let's say it is &list_of_table_names

 

Then

 

%macro do_all(table_names=);
%do i=1 %to %sysfunc(countw(&table_names));
    %let this_table_name=%scan(&table_names,&i,%str( ));
    data a;
         set &this_table_name;
         ... your calculations go here ...
     run;
     proc append base=all new=a;
     run;
%end;
%mend;
%do_all(table_names=&list_of_table_names)
--
Paige Miller
sathya66
Barite | Level 11

Thanks  Miller,

I have different approach , please can you review .

%macro loopOverDatasets(inLibref);  
    ods output Members=Members;
 proc datasets library=&inLibref memtype=data;
 run;
 quit;
    %local datasetCount iter inLibref inMember;
    /*get number of datasets*/
    proc sql noprint;
        select count(*)
         into :datasetCount
        from WORK.Members;
    quit;

    /*initiate loop*/
    %let iter=1;
    %do %while (&iter.<= &datasetCount.);
        
        data _NULL_;
            set WORK.Members (firstobs=&iter. obs=&iter.); *only read 1 record;
         
            call symput("inMember",strip(Name));
           
        run;

        /* applying my logic to the dataset*/
        data &inLibref..&inMember.; 
            set &inLibref..&inMember.;
          %testall(&inLibref..&inMember.);
        run;



        /*increment the iterator of the loop*/
        %let iter=%eval(&iter.+1);
    %end;
%mend;

/*call the macro*/
%loopOverDatasets(work);

Thanks,

 

SS

PaigeMiller
Diamond | Level 26

Thanks  Miller,

I have different approach , please can you review .

 

Why don't you execute the code and see if it works?

--
Paige Miller
sathya66
Barite | Level 11
yes,It is working and I accepted it.
Thanks,
SS
Ksharp
Super User
%macro testall(dsn);
 %put Table= &dsn ;
%mend;
data _null_;
 set sashelp.vmember(where=(memtype='DATA'));
 call execute(cats('%testall(',libname,'.',memname,')'));
run;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 851 views
  • 1 like
  • 3 in conversation