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-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
  • 2 replies
  • 734 views
  • 0 likes
  • 2 in conversation