BookmarkSubscribeRSS Feed
Saidaral
Calcite | Level 5

Hi, I'm using SAS 9.4. I have a pretty large data set (Millions of data) and I have to find the max value in the VAR1 and then return the value of VAR2, for that max value I found before. Both variables are in the same data set Call B1.

 

I already have created a new data set with the max value of VAR1 (Call US)

 

my code for the max values of VAR1 was: 

 

Proc means data=WORK.B1 max nway;

var VAR1;

OUTPUT out = US (drop= _TYPE_ _FREQ_)

Max = / autoname;

run;

 

Thank you so much for your help. 

3 REPLIES 3
PeterClemmensen
Tourmaline | Level 20

Hi and welcome to the SAS Communities 🙂

 

Here is an SQL Approach based on the SASHELP.CLASS data set

 

proc sql;
   create table want as
   select name
   from sashelp.class
   having height=max(height);
quit;

So in your case, it will be something like this

 

proc sql;
   create table want as
   select var2
   from B1
   having var1=max(var1);
quit;

 

 

PaigeMiller
Diamond | Level 26

@Saidaral wrote:

Hi, I'm using SAS 9.4. I have a pretty large data set (Millions of data) and I have to find the max value in the VAR1 and then return the value of VAR2, for that max value I found before. Both variables are in the same data set Call B1.

 

I already have created a new data set with the max value of VAR1 (Call US)

 

my code for the max values of VAR1 was: 

 

Proc means data=WORK.B1 max nway;

var VAR1;

OUTPUT out = US (drop= _TYPE_ _FREQ_)

Max = / autoname;

run;

 

Thank you so much for your help. 


In PROC MEANS, use the MAXID option of the OUTPUT statement.

 

OUTPUT out = US (drop= _TYPE_ _FREQ_) 
    Max = var1_max 
    maxid(var1(var2))=var2_at_max_var1;

 

 

--
Paige Miller
hashman
Ammonite | Level 13

It can be done in a single-pass DATA step:

data have ;                         
  input var1 var2 ;                 
  cards ;                           
10 20                               
18 21                               
11 22                               
19 23                               
17 24                               
12 25                               
16 26                               
13 27                               
15 28                               
14 29                               
run ;                               
                                    
data want (drop = _:) ;             
  do until (z) ;                    
    set have end = z ;              
    if var1 <= _max1 then continue ;
    _max1 = var1 ;                  
    _var2 = var2 ;                  
  end ;                             
  var1 = _max1 ;                    
  var2 = _var2 ;                    
run ;                               

Kind regards

Paul D.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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