BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
AmirSari
Quartz | Level 8

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

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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

4 REPLIES 4
Reeza
Super User

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;

 

 

AmirSari
Quartz | Level 8
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
Reeza
Super User

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

 

var1/ CALCULATED num_years as var3

 

AmirSari
Quartz | Level 8
That solved the problem!
Thank you!

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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