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

Dear experts,

 

First, I am on version 7.1. 

The following simple example, I would like to make it more flexible / dynamic: 

 

PROC SQL;

SELECT           MAX(Var1)
               , MAX(Var2)
               , Max(Var3)

FROM            My_Table

;

QUIT;

To make it more dynamic:

 

 

%LET Variable_List = Var1 Var2 Var3;

PROC SQL;

SELECT MAX(&Variable_List)
FROM   My_Table
;

QUIT;

This however doesn't work, but I'd like it to function as the first manually written statement. And it should also be flexible in the way that if I give in 4 or 5 variables (var4 var5), that it will still work. This can probably be solved by using a %Do loop, but I haven't been able to make it work. 

 

Could you give me some advice on how to do this?

 

Thank you!

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

If this is all you want to do, you could move away from PROC SQL:

 

%let Variable_List = Var1 Var2 Var3;

proc means data=my_table max;
   var &variable_list;
run;
   

PROC MEANS is very flexible.  You can create an output data set instead of a report:

 

%let Variable_List = Var1 Var2 Var3;

proc means data=my_table noprint;
   var &variable_list;
   output out=want (keep=&variable_list) max=;
run;

There are also ways to keep additional variables in the output if that is a requirement.  But you would need to spell out any additional requirements.

View solution in original post

4 REPLIES 4
mkeintz
PROC Star

How about:

 

%let mxlist=max(age),max(height),max(weight);
proc sql;
  select &mxlist from sashelp.class;
quit;
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Astounding
PROC Star

If this is all you want to do, you could move away from PROC SQL:

 

%let Variable_List = Var1 Var2 Var3;

proc means data=my_table max;
   var &variable_list;
run;
   

PROC MEANS is very flexible.  You can create an output data set instead of a report:

 

%let Variable_List = Var1 Var2 Var3;

proc means data=my_table noprint;
   var &variable_list;
   output out=want (keep=&variable_list) max=;
run;

There are also ways to keep additional variables in the output if that is a requirement.  But you would need to spell out any additional requirements.

PaigeMiller
Diamond | Level 26

I certainly agree with @Astounding who points out that in PROC MEANS (or it's twin sister PROC SUMMARY), you don't need any looping, and provides the simplest coding.

 

Nevertheless, in case the problem is more complicated than the original example and it needs to be done in SQL:

 

%macro dothis;
    %let variable_list=var1 var2 var3;
    proc sql;
        select 
        %do i=1 %to %sysfunc(countw(&variable_list));
            %let thisvar=%scan(&variable_list,&i,%str( ));
            max(&thisvar)
            %if &i<%sysfunc(countw(&variable_list)) %then %str(,);
        %end;
        from my_table;
    quit;
%mend dothis;
%dothis
        

So again, you can see this is a lot of coding, and a lot of opportunity for errors, and I strongly urge you to consider the much simpler PROC MEANS/PROC SUMMARY solution.

--
Paige Miller
Kazzie
Calcite | Level 5
Thanks for the code and your advice. I indeed accepted the solution from @Astounding

Yours however is very useful for my overall understanding!

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 4 replies
  • 1967 views
  • 1 like
  • 4 in conversation