DATA Step, Macro, Functions and more

First by Groups

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 14
Accepted Solution

First by Groups

Hello 

 

I have a dataset like this currently;

id term program
1 201102 Arts
1 201103 Arts
1 201201 Metal
2 200701 Arts
2 200702 Arts
3 201003 Arts
3 201003 Arts
3 201003 Arts
3 201201 Fashion
3 201202 Fashion

 

I want to create a new variable called "First_Term" and identify the first term by program, term and id. It should look like this.

 

id term program First_term
1 201102 Arts    First term
1 201103 Arts    Returning
1 201201 Metal  First term
2 200701 Arts    First term
2 200702 Arts    Returning
3 201003 Arts    First term
3 201003 Arts    Returning
3 201003 Arts    Returning
3 201201 Fashion First term
3 201202 Fashion Returning

 

I do not know how to do this? Please help.

Thanks.


Accepted Solutions
Solution
‎08-24-2017 03:11 PM
Super User
Posts: 23,237

Re: First by Groups

It's very similar to this question, except you want a character value. 

https://stats.idre.ucla.edu/sas/faq/how-can-i-create-an-enumeration-variable-by-groups/

 

If you use the method in the link, you can then recode 1 to "First" and anything else to "Returning".

 

 

View solution in original post


All Replies
Solution
‎08-24-2017 03:11 PM
Super User
Posts: 23,237

Re: First by Groups

It's very similar to this question, except you want a character value. 

https://stats.idre.ucla.edu/sas/faq/how-can-i-create-an-enumeration-variable-by-groups/

 

If you use the method in the link, you can then recode 1 to "First" and anything else to "Returning".

 

 

Occasional Contributor
Posts: 14

Re: First by Groups

Thank you to both. I looked at Reeza's article and replaced with string and worked like a charm. Earlier I was using the same code only but to use 'and' instead of 'or'.

Thanks.

Super User
Posts: 6,626

Re: First by Groups

[ Edited ]

In the long term, @Reeza is 100% right in that you need to study and learn these tools.  However, beginning with a problem that requires your data sorted by 3 variables might be a little daunting.  So here is one way:

 

proc sort data=have;

by id program term;

run;

 

data want;

set have;

by id program;

if first.program then first_term='First term';

else first_term='Returning';

run;

 

I made some assumptions about what is really needed here, since the rules for assigning "First term" vs. "Returning" don't appear (to me) to be consistent in your example.

☑ This topic is solved.

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

Discussion stats
  • 3 replies
  • 109 views
  • 2 likes
  • 3 in conversation