BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
stataq
Quartz | Level 8

Hi all,

 

I am trying to get max value by group and assign it to a set of macro variables. Can anyone please help?

 

My codes were able to get the max value for each group.(might not be a smart way. If you know a better way, please share your thoughts. thanks.)  but how can create macro variables base on the groups we have?

data have;
input group $  value;
datalines;
g1 30
g2 37
g2 76
g3 22
g3 84
g4 15
g4 21
g4 94
g4 73
;
run;

proc sort;
by group descending value  ;
run; 
data want (keep= group max_v);
   set have;
   by group;
   retain max_v;
   if first.group then max_v= value;
run;

proc sort nodupkey;
by _all_;
run;

stataq_0-1727829372964.png

 

here we have 4 group, the ideal macro variables are

%let grp1=30; %let grp2=76;  %let grp3=84; %let grp4=94;

Is there a smart way to build those macro variables automatically?

1 ACCEPTED SOLUTION

Accepted Solutions
AhmedAl_Attar
Ammonite | Level 13

Try this

data have;
input group $  value;
datalines;
g1 30
g2 37
g2 76
g3 22
g3 84
g4 15
g4 21
g4 94
g4 73
;
run;

proc sql noprint;
	select max(value)
	into :grp1-
	from have
	group by group;
quit;

View solution in original post

8 REPLIES 8
AhmedAl_Attar
Ammonite | Level 13

Try this

data have;
input group $  value;
datalines;
g1 30
g2 37
g2 76
g3 22
g3 84
g4 15
g4 21
g4 94
g4 73
;
run;

proc sql noprint;
	select max(value)
	into :grp1-
	from have
	group by group;
quit;
stataq
Quartz | Level 8

@AhmedAl_Attar Thanks for the prompt answer. What if there is null value in group? How to assign macro variable only for non-null group?

data have;
input group $  value;
datalines;
g1 30
g2 37
g2 76
g3 22
g3 84
g4 15
g4 21
   94
g4 73
;
run;
AhmedAl_Attar
Ammonite | Level 13

Hi @stataq 

Typically, the missing value would be considered as a fifth grouping value, and the below SQL statement would generate 5 macros (grp1 -- grp5), where grp1 represents the null/missing value. 

proc sql noprint;
	select max(value)
	into :grp1-
	from have
	/*Where group is not null*/ /* This would ignore null/missing values */
	group by group;
quit;
%put _user_;

 But if you want to exclude the null/missing group value, then use where clause as @Kurt_Bremser has suggested already.

Kurt_Bremser
Super User
proc means data=have;
class group;
var value;
output out=want max()=;
run;

data _null_;
set want;
call symputx(group,value);
run;

But I seriously question the need for macro variables; for all purposes I see, the want dataset is better suited.

What are you intending to do with those macro variables?

PaigeMiller
Diamond | Level 26

Hello, @stataq , excellent advice from @Kurt_Bremser , please tell us (as he said) the big picture. Why you need these in macro variables? What are you going to do with the macro variables? (And in fact, knowing the big picture is something you should tell us every time you post a question here, we need explanation of the problem, explanation of the big picture, rather than a simple question about code). When we have that big picture explanation, we can often come up with a better solution than the one you are trying to program. For example, if you want to subtract the mean from the data in each group (that's the big picture description), no macro variables are needed, you can use PROC STDIZE and get it done with one PROC call.

--
Paige Miller
stataq
Quartz | Level 8
I am learning how to set up macro. Just try to understand how to extract the needed info from a dataset and put it into a macro variable.
PaigeMiller
Diamond | Level 26

Part of learning macros is learning when NOT to use macros. As @Kurt_Bremser said: "But I seriously question the need for macro variables; for all purposes I see, the want dataset is better suited."

--
Paige Miller

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 8 replies
  • 1213 views
  • 3 likes
  • 4 in conversation