Using a PUT Function to Create a New Variable - SAS Programming

Although the previous method(using FORMAT in a PROC step) works for the purpose illustrated without having to create a new variable,you may indeed have the need to create a new variable whose values represent the receded values.You can,of course,use the method noted in Example , or you can make a minor change to the program in Example to create the new variable.To do this, you employ the powerful PUT function.This function takes the form:

char_var=PJY (arg_var,any_ftnt.}

This statement creates a character variable char_var(note that the PUT function always creates a character variable regardless of the format specification) whose value is the formattedvalue (using anyjmt.} of the argument variable arg_var.For example,the expression

GRADE=PUT (SCORE,SCOREFMT.);

creates a character variable GRADE with values of Fail,Low Pass,etc. You can then use this variable in any subsequent procedure you like.For example:

Example

DATA NEW;
SET GRADES;
CATEGORY=PUT (SCORE,SCOREFMT.};
RUN;
PROC PRINT DATA=NEW;
TITLE 'Example'?
RUN;

The resulting output is as follows:

Output from Example-Using a PUT Function to Create a New Variable

Output from Example-Using a PUT Function to Create a New Variable

Problems

  • You have a SAS data set called HTWT which contains variables ID,HEIGHT,and WEIGHT. You want a crosstabulation of HEIGHT by WEIGHT, where HEIGHT and WEIGHT are to be grouped as follows (heights and weights are all integers):
HEIGHT groupings: O to 36= 1
37 to 48 = 2
49 to 60 = 3
>60= 4
WEIGHT groupings: 0 to 100 = 1
101 to 200 = 2
> 200 = 3

Use all four recocting methods described in this chapter to either create a new SAS data set HTWT_2 which contains grouping variables HT_GROUP and WT_GROUP,or to do the grouping in the PROC step.Then write the code to produce the crosstabulations.

Hint:The PROC FREQ code to perform a crosstabulation of VAR_X by VAR_Y is:

PROC FREQ;
TABLES VAR_X*VAR_Y;
RUN;

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

SAS Programming Topics