Sentry Page Protection
Variable Attributes [13-17]
Creating your own format
In SAS, there are about 150 built-in formats. However, you often need to create your own custom format to suit your data.
Let’s take a look at this example:
Example
Let’s take a look at this example:
Example
The SURVEY data set contains 4 questions: Q1, Q2, Q3, and Q4.
The four questions are taken from a customer service survey where the responses are coded as:
- 1 = Strongly Disagree
- 2 = Disagree
- 3 = Neutral
- 4 = Agree
- 5 = Strongly Agree
The variables (i.e. Q1-Q4) show only the numbers but not the coded values.
This could cause confusion when analyzing the data, as the analyst might not understand what meaning each integer represents.
Proc Format
You can use a procedure called Proc Format to create a custom format for this.
Example
Proc Format;
Value Ques
1 = "Strongly Disagree"
2 = "Disagree"
3 = "Neutral"
4 = "Agree"
5 = "Strongly Agree";
Run;
The Proc Format above creates a custom format called QUES.
You can now apply the format (QUES.) to the variables that contain the survey questions.
Example
Data Survey2;
Set Survey;
Format Q1 Q2 Q3 Q4 Ques.;
Run;
The variables now display the values according to the QUES format that you have created.
Important Note
When dealing with categorical data, such as
- Gender (Male or Female)
- Grade (A, B, C, D, E, F)
- Color (Red, Blue, Green)
It is almost always better to keep the data in numeric variables with a custom format.
Exercise
Copy and run the code from the yellow box below:
The PROFILE data set contains 3 variables:
- ID
- Gender
- Race
The Gender and Race are stored under a character variable, which is not preferred in our analysis.
Create two new variables for Gender and Race that contain the Gender and Race information in numeric values. Assign a proper format to the newly created variables.
Need some help?
HINT:
You cannot change the variable type (numeric or character) once the variable is created. You must create a new variable if you need to convert the variable from one type into another.
SOLUTION:
Proc Format;
Value Gender 1 = "Male"
2 = "Female";
Value Race 1 = "Caucasian"
2 = "Black"
3 = "Asian";
Run;
Data Profile2;
Set Profile;
Format Gender2 Gender. Race2 Race.;
If Gender = "Male" then Gender2 = 1;
else if Gender = "Female" then Gender2 = 2;
If Race = "Caucasian" then Race2 = 1;
else if Race = "Black" then Race2 = 2;
else if Race = "Asian" then Race2 = 3;
Drop Gender Race;
Run;
Fill out my online form.