I have a dataset that looks like this (except for the last variable) and am wanting to create that last variable.
Essentially, the NUMBER variable is nested in ID, and one person (ID) can have multiple NUMBERs that can last up to 2000 observations, and all I want to do is create a variable that says which order NUMBER it is for a specific ID, for the entire dataset.
Here is what the dataset looks like:
ID NUMBER VARABLE_DESIRED
1 100 1
1 100 1
1 100 1
1 101 2
1 102 2
1 103 3
2 104 1
2 104 1
2 105 2
2 106 3
2 106 3
3 119 1
3 121 2
4 125 1
4 125 1
Thank you!
Assuming you incorrectly specified the variable_desired for id=1, number=102, then the following will do what you want:
data have;
input ID NUMBER;
cards;
1 100
1 100
1 100
1 101
1 102
1 103
2 104
2 104
2 105
2 106
2 106
3 119
3 121
4 125
4 125
;
data want;
set have;
by id number;
if first.id then variable_desired=1;
else if first.number then variable_desired+1;
run;
Art, CEO, AnalystFinder.com
Assuming you incorrectly specified the variable_desired for id=1, number=102, then the following will do what you want:
data have;
input ID NUMBER;
cards;
1 100
1 100
1 100
1 101
1 102
1 103
2 104
2 104
2 105
2 106
2 106
3 119
3 121
4 125
4 125
;
data want;
set have;
by id number;
if first.id then variable_desired=1;
else if first.number then variable_desired+1;
run;
Art, CEO, AnalystFinder.com
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.