Hello,
I am working on a homework assignment, and part of the assignment is to take two variables 'FirstNm' and 'LastNm' and combine them into one variable titled 'Name' with the format last name, first name. How do I do this?
@BLT2023 wrote:
Thank you! I guess I miscounted. The output came out as "Taylor,Daniel"- how do I get a space in between the comma and the D?
I give you a hint: it is a tiny change to the CATX function first argument. I give another hint, which was previously given by @Reeza: really, look at the documentation for CATX, its all explained in there.
a follow-up question: How do I do this for more than one name?
This is simply done in a DATA step, where you use the SET command to allow it to work on another SAS data set with lots of names.
One thing you need to be very aware of with SAS is that FORMAT has a very specify meaning related to display of values using a defined Format (read up on the procedure). The format at display time applies rules for displaying a value.
You are discussing layout or order, not format.
That involves concatenation in this case, the formal term for "sticking text together".
You should define the length of the new variable before assigning values as the approach chosen could create extremely long values.
You aren't clear whether the result should have a comma, a space or a comma and a space as a result.
It is best to provide an actual example. Such as Firstnm='Bob' , Lastnm='Smith' and want Name='Smith, Bob'. Now we can see that you want to insert a comma and a space (if that is in fact the desired result).
What have you tried so far? I should think that your instructor provided at least some clues.
If not, one clue: CATX function would be my preferred tool.
I have tried:
data WORK.stjohns; FirstNm = 'Daniel'; LastNm = 'Taylor'; length Name $ 12; Name = catx(',', LastNm, FirstNm); put _all_; run;
It gave me nothing in my new variable. I am looking for Firstnm='Daniel' , Lastnm='Taylor' and want Name='Taylor, Daniel'.
Your length statement is shorter than the desired character string. Change the length statement to allow bigger character strings.
Thank you! I guess I miscounted. The output came out as "Taylor,Daniel"- how do I get a space in between the comma and the D?
@BLT2023 wrote:
Thank you! I guess I miscounted. The output came out as "Taylor,Daniel"- how do I get a space in between the comma and the D?
I give you a hint: it is a tiny change to the CATX function first argument. I give another hint, which was previously given by @Reeza: really, look at the documentation for CATX, its all explained in there.
a follow-up question: How do I do this for more than one name?
This is simply done in a DATA step, where you use the SET command to allow it to work on another SAS data set with lots of names.
I am a bit confused by what you mean when you say (in reference to SET) 'allow it to work on another SAS data set with lots of names.' It is the same data set (I'musing data lines), but there are 2 more names within the data lines. that need catx. Does your statement about SET still apply? Thank you for clarifying.
data class;
set sashelp.class (keep=name rename=name=first_name); *this is your input data set;
last_name = "Flinstone"; *make a fake last name as there's no names in the data;
name = catx(", ", last_name, first_name); *create full name;
run;
proc print data=class;
run;
@BLT2023 wrote:
I am a bit confused by what you mean when you say (in reference to SET) 'allow it to work on another SAS data set with lots of names.' It is the same data set (I'musing data lines), but there are 2 more names within the data lines. that need catx. Does your statement about SET still apply? Thank you for clarifying.
Show us the SAS data set you are working with. (We don't need all of it, for example 5 lines ought to be good enough)
Also, a follow-up question: How do I do this for more than one name?
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.