Monday, April 17, 2017

Hack 4.19 Up-Coding Text Variables with the VVALUE Function

SAS Programming Professionals,

Did you know that you can use a SAS format to up-code text variables based on a numeric response variable?

The VVALUE function returns a character value that is the results of bouncing a numeric variable’s value against a format.  The format for the VVALUE function is:

VVALUE (variable)

Let’s look at an example:

/* Create potato chip format */
proc format;
      value chiptype

      1 = "Ruffles"
      2 = "Lay's"
      3 = "Kettle"
      4 = "Miss Vickies"
      5 = "Pringles"
      other = "Don't Know"

/* Create example data set */
data class;
set  sashelp.class;

Chip_Response = mod(age, 10);

format Chip_Response chiptype.;


/* Upcode value of Chip_Response_Text */
data class_Response;
set  class;

length Chip_Response_Text $12.;

Chip_Response_Text = vvalue(Chip_Response);


proc print noobs data=class_Response;
title1 "Results of the Potato Chip Question";
var Name Age Chip_Response Chip_Response_Text;
format Chip_Response 8.;

First we create the CHIPTYPE format to associate numeric responses with the related potato chip type as per the questionnaire.  The first DATA step creates the Chip_Response variable that represents the various subject’s response to the potato chip question in the survey.

The second DATA step uses the VVALUE function to assign the correct response text value to Chip_Response_Text, based on the value found in variable Chip_Response.  Since the argument to VVALUE is Chip_Response, the function looks to the CHIPTYPE format for the values that should be returned.  It does so because CHIPTYPE is the format associated with variable Chip_Response.  Chip_Response values of 1 result in Chip_Response_Text values of “Ruffles”, values of 2 result in “Lay’s”, and so forth; as per the CHIPTYPE format.

The output of the PROC PRINT looks like this:

Results of the Potato Chip Question

   Chip_      Response_
Name       Age    Response    Text
Alfred      14           4    Miss Vickies
Alice       13           3    Kettle
Barbara     13           3    Kettle
Carol       14           4    Miss Vickies
Henry       14           4    Miss Vickies
James       12           2    Lay's
Jane        12           2    Lay's
Janet       15           5    Pringles
Jeffrey     13           3    Kettle
John        12           2    Lay's
Joyce       11           1    Ruffles
Judy        14           4    Miss Vickies
Louise      12           2    Lay's
Mary        15           5    Pringles
Philip      16           6    Don't Know
Robert      12           2    Lay's
Ronald      15           5    Pringles
Thomas      11           1    Ruffles
William     15           5    Pringles

Note that if you do not specify a length for a variable created with the VVALUE function, the variable length will default to 200 characters.  Ouch!

Best of luck in all your SAS endeavors!

(aka Michael A. Raithel)
Author of the new cult classic for computer programmers:  It Only Hurts When I Hit <ENTER>
Print edition: 
Kindle edition: 

The hack above is an excerpt from the book:  Did You Know That?  Essential Hacks for Clever SAS Programmers