BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
cosmid
Lapis Lazuli | Level 10

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;
1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
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;

 

View solution in original post

5 REPLIES 5
cosmid
Lapis Lazuli | Level 10
I'm sorry. What I meant by increasing the value by 1 based on the max value, is to increase the value by 1 in the first row in each group and then for that group the value keeps getting increased by 1 from there on.
Reeza
Super User
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;

 

cosmid
Lapis Lazuli | Level 10
Wow! I didn't know you can use a proc means like that! Really learned a lot, thank you!

This works great for the sample data! Is there a way to keep other columns while increasing the value by 1? It seems the proc freq dropped other columns when I test it.
Reeza
Super User
You can look at the ID statement in PROC MEANS to keep other variables or you can remerge it with your original data set to keep other columns.
There is no PROC FREQ in my code.
Ksharp
Super User
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;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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