- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
I have looked at a number of conditional formatting questions, but none seem to quite get what I'm after. I'm frustrated, because I believe I went to a conference where someone talked about using proc format but being able to apply it conditionally, and I think I've successfully done it in the past but can't find a program using that method. I would also be willing to use proc report, but it's newish to me!
What I'm trying to do is apply a format to var2 based on the value of var1. So if var1 is 'food' then var2 should be 'broccoli'. If var1 is 'auto' then var2 should be 'sedan'. Obvious oversimplification, but that's what I'm trying to do.
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
You cannot change the format attached to a variable. You can decided which format to use using logic however.
Let's say you have two formats named FOOD and AUTO that convert numbers into names of food or car types.
So you use the different formats to decode the numbers into a character variable.
length result $20;
if var1='food' then result=put(var2,food20.);
else if var1='auto' then result=put(var2,auto20.);
You could even use the PUTN() function to simplify the code.
result=putn(var2,cats(var1,'20.'));
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
It seems that you are looking for a format similar to:
proc format lib=work; /* or any other library */
value $new
'food' = 'broccoli'
'auto' = 'sedan''
...
; run;
data want;
set have;
length var2 $20;
var2 = put(var1, $new.);
run;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
You cannot change the format attached to a variable. You can decided which format to use using logic however.
Let's say you have two formats named FOOD and AUTO that convert numbers into names of food or car types.
So you use the different formats to decode the numbers into a character variable.
length result $20;
if var1='food' then result=put(var2,food20.);
else if var1='auto' then result=put(var2,auto20.);
You could even use the PUTN() function to simplify the code.
result=putn(var2,cats(var1,'20.'));
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thanks Tom - that solved my issue. I still feel like I remember that there is a way, in the proc format itself, to apply the conditions based on the value of the variable being formatted. But, it was a long time ago!
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@jbatch wrote:
Thanks Tom - that solved my issue. I still feel like I remember that there is a way, in the proc format itself, to apply the conditions based on the value of the variable being formatted. But, it was a long time ago!
That is only possible if the range of values do not overlap. So if 1-10 is used for food and 100-200 is use for autos you could make a format like this:
proc format;
value combo
1-10=[food.]
100-200=[auto.]
;
run;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
If your first variable, VAR1, is a nice SAS-compatible name, you can use the variable value as a format name, and then use PUTC or PUTN (depending on the type of VAR2), e.g.:
data have;
input var1 $ var2;
cards;
auto 1
food 1
;run;
proc format;
value auto 1='sedan'
;
value food 1='broccoli';
;
run;
data want;
set have;
formatted_value=putn(var2,cats(var1,'20.'));
run;