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;

sas-innovate-white.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.

 

Early bird rate extended! Save $200 when you sign up by March 31.

Register now!

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
  • 671 views
  • 3 likes
  • 3 in conversation