Sentry Page Protection
SAS Functions [9-14]
Combining Character Variables
You can use the double stroke (||) to combine character variables in SAS.
Let's take a look at the example below:
The TEXT data set above contains 2 variables: CHR1 and CHR2.
[Reminder: To see the TEXT data set on SAS Studio, run the code in the yellow box above]
Now, we're going to combine the two variables by using the double stroke (||).
Example
Data Text2;
Set Text;
Chr3 = Chr1 || Chr2;
Run;
Note: the stroke button is located above the Enter key on the majority of the keyboard.
TRIM Function
Sometimes, a little prep work might be needed before combining the character variables.
Copy and run the code from the yellow box below to get the PROFILE data set on SAS Studio.
The PROFILE data set above contains 2 sets of ID: SiteID and PatientID.
The 2 IDs are combined to form a unique identifier for each patient.
Example
Data Profile2;
Set Profile;
ID = SiteID || PatientID;
Run;
When combining the two IDs, there is an extra space between the two variables.
The extra space comes from the trailing space at the end of the SiteID variable.
In order remove the extra space, you must use a TRIM function to eliminate the trailing space from SiteID.
Example
Data Profile3;
Set Profile;
ID = TRIM(SiteID) || PatientID;
RUn;
The trailing space is now removed.
Exercise
Copy and run the FIRSTLAST data set from the yellow box below.
Copy and run the FIRSTLAST data set from the yellow box below.
Write a SAS program to combine the first name and last name into one variable.
The first and last name should be separated by a comma:
E.g. Justin, Jeever
Create any data set or variables if necessary.
Need some help?
HINT:
Remember to add a comma between the first and last name.
SOLUTION:
Data FirstLast2;
Set FirstLast;
Name = TRIM(First) || ", " || Last;
Run;
Fill out my online form.