Max value of a column without grouping

Accepted Solution Solved
Reply
Contributor
Posts: 24
Accepted Solution

Max value of a column without grouping

Hello,

I have a dataset similar to the below. I need to find the cumulative number of years that a person appears in my dataset, put it in another column in the same data set, say VAR2, and divide VAR1 by that column, which would be VAR3.

Year

personID

VAR1

VAR2

VAR3

2000

1009

2

3

2/3

2001

1009

3

3

3/3

2002

1009

5

3

5/3

2000

1020

1

4

1/4

2001

1020

4

4

4/4

2002

1020

5

4

5/4

2003

1020

6

4

6/4

2000

1023

2

2

2/2

2001

1023

3

2

3/2

 

What I have so far:

data want;

set have;

by personID;

if first. personID then num=1;

else num + 1;

run;

This gives me a column, num, that contains number of years that a person appear in my table. But I cannot fidure out how to find max of this column and put it in another column without grouping.

Any help would be greatly appreciated!

Thank you


Accepted Solutions
Solution
‎11-30-2016 10:54 PM
Super User
Posts: 19,063

Re: Max value of a column without grouping

I'm not sure what you mean about not grouping. I would recommend SQL in this case as it's easier and a single step. 

 

FYI - please post your data as text, preferably a data step. If I can't copy and paste to a decent format I'm NOT typing out your data. Or testing any code since I don't have data.

 

proc sql;
create table want as
select a.*, count(*) as num_years, var1/num_years as var3
from have as a
group by personID
order by personID, year;
quit;

 

 

View solution in original post


All Replies
Solution
‎11-30-2016 10:54 PM
Super User
Posts: 19,063

Re: Max value of a column without grouping

I'm not sure what you mean about not grouping. I would recommend SQL in this case as it's easier and a single step. 

 

FYI - please post your data as text, preferably a data step. If I can't copy and paste to a decent format I'm NOT typing out your data. Or testing any code since I don't have data.

 

proc sql;
create table want as
select a.*, count(*) as num_years, var1/num_years as var3
from have as a
group by personID
order by personID, year;
quit;

 

 

Contributor
Posts: 24

Re: Max value of a column without grouping

Thanks for your reply. By not grouping I mean I need all information! Anyway the code you have provided is not working. The log says that num_years was not found.
Here is the data as text;
Year personID VAR1 VAR2 VAR3
2000 1009 2 3 2/3
2001 1009 3 3 3/3
2002 1009 5 3 5/3
2000 1020 1 4 1/4
2001 1020 4 4 4/4
2002 1020 5 4 5/4
2003 1020 6 4 6/4
2000 1023 2 2 2/2
2001 1023 3 2 3/2

Thanks again
Super User
Posts: 19,063

Re: Max value of a column without grouping

[ Edited ]

When using a statistic calculated in the same query it needs a CALCULATED keyword in front.

 

var1/ CALCULATED num_years as var3

 

Contributor
Posts: 24

Re: Max value of a column without grouping

That solved the problem!
Thank you!
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 206 views
  • 0 likes
  • 2 in conversation