DATA Step, Macro, Functions and more

Creating variable that shows which number of subgroup

Accepted Solution Solved
Reply
New Contributor
Posts: 2
Accepted Solution

Creating variable that shows which number of subgroup

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!


Accepted Solutions
Solution
‎01-15-2017 02:48 PM
PROC Star
Posts: 7,362

Re: Creating variable that shows which number of subgroup

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


All Replies
Solution
‎01-15-2017 02:48 PM
PROC Star
Posts: 7,362

Re: Creating variable that shows which number of subgroup

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

 

New Contributor
Posts: 2

Re: Creating variable that shows which number of subgroup

You assumed right Smiley Happy thank you so much for this!
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

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