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

Hi, I am trying to create a loop that I can use that will get the min and max value from each column in multiple tables.  Can this be done?

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

See this:

/* create an example table */
data class;
set sashelp.class;
run;

/* create a control dataset that holds all our datasets to be investigated */
data datasets;
input libname :$8. memname :$32.;
libname = upcase(libname);
memname = upcase(memname);
/* SAS keeps all libnames and memnames in uppercase */
datalines;
work class
;

/* a macro to calculate the min and max for all nuemric variables in a dataset */
%macro get_minmax(libname=,memname=);
data _null_;
set sashelp.vcolumn (
  where=(libname = "&libname." and memname = "&memname." and type = 'num')
) end=eof;
if _n_ = 1
then call execute('
  proc means data=' !! strip(libname) !! '.' !! strip(memname) !! ' min max;
  var
');
call execute(' ' !! strip(name));
if eof
then call execute('
  ;
  run;
');
run;
%mend;

/* a data step that executes the above macro for all datasets in our reference library */
data _null_;
set datasets;
call execute(cats('%nrstr(%get_minmax(libname=',libname,',memname=',memname,'))'));
run;

Result:

           Die Prozedur MEANS

Variable         Minimum         Maximum
----------------------------------------
Age           11.0000000      16.0000000
Height        51.3000000      72.0000000
Weight        50.5000000     150.0000000
----------------------------------------

View solution in original post

6 REPLIES 6
PeterClemmensen
Tourmaline | Level 20

Yes. What does your data look like?

Kurt_Bremser
Super User

See this:

/* create an example table */
data class;
set sashelp.class;
run;

/* create a control dataset that holds all our datasets to be investigated */
data datasets;
input libname :$8. memname :$32.;
libname = upcase(libname);
memname = upcase(memname);
/* SAS keeps all libnames and memnames in uppercase */
datalines;
work class
;

/* a macro to calculate the min and max for all nuemric variables in a dataset */
%macro get_minmax(libname=,memname=);
data _null_;
set sashelp.vcolumn (
  where=(libname = "&libname." and memname = "&memname." and type = 'num')
) end=eof;
if _n_ = 1
then call execute('
  proc means data=' !! strip(libname) !! '.' !! strip(memname) !! ' min max;
  var
');
call execute(' ' !! strip(name));
if eof
then call execute('
  ;
  run;
');
run;
%mend;

/* a data step that executes the above macro for all datasets in our reference library */
data _null_;
set datasets;
call execute(cats('%nrstr(%get_minmax(libname=',libname,',memname=',memname,'))'));
run;

Result:

           Die Prozedur MEANS

Variable         Minimum         Maximum
----------------------------------------
Age           11.0000000      16.0000000
Height        51.3000000      72.0000000
Weight        50.5000000     150.0000000
----------------------------------------
RALL
Obsidian | Level 7
Thanks for this, it did the trick.
RALL
Obsidian | Level 7

Quick question, what would I have to add to the code to get the table names to appear?

Kurt_Bremser
Super User

Maxim 2: Read the Log. Everything that's happened is in there.

 

If you need it in the output, add a title:

%macro get_minmax(libname=,memname=);
data _null_;
set sashelp.vcolumn (
  where=(libname = "&libname." and memname = "&memname." and type = 'num')
) end=eof;
if _n_ = 1
then call execute('
  title "Means for ' !! strip(libname) !! '.' !! strip(memname) !! '";
  proc means data=' !! strip(libname) !! '.' !! strip(memname) !! ' min max;
  var
');
call execute(' ' !! strip(name));
if eof
then call execute('
  ;
  run;
');
run;
%mend;

 

Ksharp
Super User

If you have many table and many variables ,you can run the following code several times by setting FIRSTOBS= and OBS= options.

 

data _null_;
 set sashelp.vcolumn(keep=libname memname name type
 where=(libname='SASHELP' and type='num') obs=100) end=last; /*change sashelp as your library*/
 if _n_=1 then call execute('proc sql; create table final_want as  '); 
 call execute(catt('select "',memname,'" as memname,','"',name,'"  as name,
min(',name,') as min,max(',name,') as max from sashelp.',memname));	/*change sashelp as your library*/
 if not last then call execute('union');
  else call execute(';quit;');
run;

sas-innovate-wordmark-2025-midnight.png

Register Today!

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.


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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 2158 views
  • 2 likes
  • 4 in conversation