Sentry Page Protection
Variable Attributes [16-17]
PUT Function
The PUT function converts a numeric variable to a character variable.
Example
Example
Let's take a look at the variable Num1.
Num1 is numeric.
A PUT function can be used to convert Num1 to a character variable.
Example
Data Conv2;
Set Conv;
Char1 = put(Num1, best.);
Run;
A new variable called Char1 is created.
It contains the identical information as Num1.
However, it is a character variable.
PUT function
The PUT function takes on two parameters:
- The variable to be converted
- The format to use for conversion
The format allows you to convert the formatted values to the character variable.
In our example, the (best.) format is used.
Char1 = put(Num1, best.);
This tells SAS to convert the values based on the internal values stored in Num1.
E.g. 1201.1234 --> 1201.1234
Let's take a look at another example.
Example
Data Conv3;
Set Conv;
Char1 = put(Num1, dollar10.2);
Run;
The (dollar10.2) format is used.
The formatted value of Num1 is converted into Char1.
Char1 will show the dollar amount with the dollar sign ($) as well as the comma separator.
E.g. 1201.1234 --> $1,201.1234
Now, let's take a look at another example.
Example
Data Conv4;
Set Conv;
Char1 = put(Num2, YYMMDD10.);
Run;
The (YYMMDD10.) format is used.
The formatted value of Num2 (e.g. 2014-10-05) is converted into Char1.
Important Note
Many people get confused about the difference between formatting a variable and converting a variable using the PUT function.
Formatting the variable does not change the type of the variable.
However, when using the PUT function, the variable type is changed from numeric into character.
Exercise
Copy and run the code from the yellow box below:
The TRTMENT data set above contains 3 variables:
- PATID: Patient ID
- INFDT: Date of Informed Consent
- TRTDT: Date of Treatment
Before receiving the treatment, each patient must sign a form of informed consent.
The informed consent form should provide adequate information for the patient to make an informed decision about the participation of the treatment.
As a clinical researcher, you noticed that some patients received treatments before they sign the informed consent form.
Find out which patient violated the procedure.
Create a data set that contains the Patient ID, as well as a comment in the structure below:
"Patient xx received his/her first treatment on MM-DD-YYYY. However, the treatment is received prior to the date of informed consent (MM-DD-YYYY)."
Need some help?
HINT:
The dates must be converted into a character variable in order to combine them with the rest of the comment.
SOLUTION:
Data Vio;
Set Trtment;
If Infdt > TrtDt then do;
Comment = "Patient " || Compress(PATID) || " received his/her first treatment on " || Put(TrtDt, MMDDYY10.) || ". However, the treatment is received prior to the date of informed consent (" || put(Infdt, MMDDYY10.) || ").";
Output;
End;
Keep Patid comment;
Run;
Fill out my online form.