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
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.