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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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

3 REPLIES 3
Reeza
Super User

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".

 

 

anandas
Obsidian | Level 7

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.

Astounding
PROC Star

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.

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
  • 3 replies
  • 661 views
  • 2 likes
  • 3 in conversation