If I have a data like this:
data sample;
input class x;
datalines;
1 1
1 2
2 1
2 2
2 3
3 5
3 6
run;
And I want the output to be:
class x
1 3
1 4
2 4
2 5
2 6
3 7
3 8
I can get the max number from each class but I don't know how to proceed to the next step by increasing them by 1.
proc sql;
select max(x) into :max_val
from sample
group by class;
quit;
proc means data=sample noprint NWAY;
class class;
var x;
output out=maxes N=N Max = Max;
run;
data want;
set maxes;
do i=1 to n;
x = max + i ;
output;
end;
keep class x;
run;
@cosmid wrote:
If I have a data like this:
data sample; input class x; datalines; 1 1 1 2 2 1 2 2 2 3 3 5 3 6 run;
And I want the output to be:
class x 1 3 1 4 2 4 2 5 2 6 3 7 3 8
I can get the max number from each class but I don't know how to proceed to the next step by increasing them by 1.
proc sql; select max(x) into :max_val from sample group by class; quit;
proc means data=sample noprint NWAY;
class class;
var x;
output out=maxes N=N Max = Max;
run;
data want;
set maxes;
do i=1 to n;
x = max + i ;
output;
end;
keep class x;
run;
@cosmid wrote:
If I have a data like this:
data sample; input class x; datalines; 1 1 1 2 2 1 2 2 2 3 3 5 3 6 run;
And I want the output to be:
class x 1 3 1 4 2 4 2 5 2 6 3 7 3 8
I can get the max number from each class but I don't know how to proceed to the next step by increasing them by 1.
proc sql; select max(x) into :max_val from sample group by class; quit;
data sample; input class x; datalines; 1 1 1 2 2 1 2 2 2 3 3 5 3 6 ; data sample; set sample; by class; if first.class then n=0; n+1; run; proc sql; create table want as select *,sum(n,max(x)) as want from sample group by class order by 1,2; quit;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.