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.
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".
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".
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.
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 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.