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

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!

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

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

 

View solution in original post

2 REPLIES 2
art297
Opal | Level 21

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

 

dtoliver
Calcite | Level 5
You assumed right 🙂 thank you so much for this!

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 906 views
  • 0 likes
  • 2 in conversation