user27008
user27008

Reputation: 610

User written formats and comparison/logical operators in SAS

I am wondering if there is a way to perform operations on formatted values of variables with user written formats. For example, I want to compare the variable food1 with the variable food2 (both have user written formats). I want to do something like this:

if food1='ice cream' and food2='pie' then ...;

This is easy enough, though I am not sure the proper way to compare these variables if 'ice cream' and 'food' are the user written format values. Lets say 'ice cream' is actually 'A' and 'pie' is actually 'B'. Is there a way to do this comparison without removing the format or making new variables or using the actual values?

Upvotes: 2

Views: 55

Answers (2)

Joe
Joe

Reputation: 63424

If you're using the data step (and not PROC SQL or similar), you can use VVALUE:

if vvalue(food1)='ice cream' and vvalue(food2)='pie' then ...

This accesses the currently defined formatted value (based on the format currently defined for the variable, which could change during the data step!). This does not require knowing what that format is.

VVALUEX is similar, but takes a character argument for the variable name (so if you don't know the variable name you want to evaluate, that's the right way to go).

Upvotes: 2

Jeff
Jeff

Reputation: 1807

This can be done the put() function. Replace "format1" and "format2" below with the name(s) of your user-written format(s).

if put(food1,format1.) ='ice cream' and put(food2,format2.) ='pie' then ...;

Upvotes: 1

Related Questions