BookmarkSubscribeRSS Feed
Amalik
Calcite | Level 5

Hi,

 

I have a variable D. Now I want to create another variable say D1 which takes the maximum value of D by each quarter.Just as below. Just unsure how to go about it  in SAS

 

DD1Quarter
011
011
111
002
002
002
2 REPLIES 2
PeterClemmensen
Tourmaline | Level 20

Here is one way

 

data have;
input D Quarter;
datalines;
0 1
0 1
1 1
0 2
0 2
0 2
0 3
2 3
1 3
;

data want;
   do until (last.Quarter);
      set have;
      by Quarter;
      if D > D1 then D1=D;
   end;
   do until (last.Quarter);
      set have;
      by Quarter;
      output;
   end;
run;

Result:

 

D  Quarter D1 
0  1       1 
0  1       1 
1  1       1 
0  2       0 
0  2       0 
0  2       0 
0  3       2 
2  3       2 
1  3       2 
Ksharp
Super User

proc sql;
create table want as
select *,max(d) as d1
 from have 
  group by Quarter;
quit;

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

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 830 views
  • 3 likes
  • 3 in conversation