Search the site...

SASCRUNCH TRAINING
  • Home
  • Member's Area
  • How to Start
  • SAS Interface
  • Creating a Data Set
  • Practical SAS Training Course
  • SAS Certified Specialist Training Program
  • Proc SQL Course
  • Introduction to Time Series Analysis
  • SAS Project Training Course
  • Full Training / Membership
  • Sign up
  • About us
  • Contact us
  • Home
  • Member's Area
  • How to Start
  • SAS Interface
  • Creating a Data Set
  • Practical SAS Training Course
  • SAS Certified Specialist Training Program
  • Proc SQL Course
  • Introduction to Time Series Analysis
  • SAS Project Training Course
  • Full Training / Membership
  • Sign up
  • About us
  • Contact us
Sentry Page Protection
Please Wait...
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:
Picture

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; 
Picture

Note: the stroke button is located above the Enter key on the majority of the keyboard.
Picture

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

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;
Picture

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;
Picture

The trailing space is now removed.

Exercise

Copy and run the FIRSTLAST data set from the yellow box below.
Picture

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

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.

Already a member? Go to member's area.