03-16-2018 02:18 PM
I have one variable that's values are either 'state' of 'country'. State meaning its a state in the united states. Country meaning it is outside of the united states.
My second variables values are state or country codes. This is the variable I want to format with two different formats based on what my first variable is.
Basically, if my first variable has a value of 'state' then I want to apply my state format to the second variable. And if my first variable has a value of 'country' then I want to apply my country format to the second variable.
As an example, if my first variable has a value of 'state' and my second variable has a value of 'TX' then the state format would make that value in my second variable read 'TEXAS'. But if first variable has a value of 'country' and my second variable has a value of 'TX' then the country format would make that value in my second variable read 'TURKMENISTAN'.
I know this next line of code is incorrect but I think it tells you what I'm trying to do
if var1='state' then var2 $state. else var2 $country.;
I'm using Enterprise Guide 7.1
03-16-2018 02:22 PM
I think you’ll need to create a new variable in this case with the formatted value.
if var1=‘state’ then desc = putc(var2, ‘$state_fmt’);
else desc = putc(var2, ‘$country_fmt’);
03-16-2018 02:30 PM
You can't do that. No more than you have some observations where the variable is numeric and others where the same variable is character. The format attached to a variable is an attribute of the variable, not of a particular observation. You will need to create a new variable to hold the decoded value.
So if you have data like:
data have ; length code $10 code_type $10 ; input code code_type ; cards; TX state USA country ;
If you have formats named $STATE and $COUNTRY you could even just use the value of CODE_TYPE to generate the format to use.
data want ; set have ; length description $100 ; description=putc(code,cats('$',code_type,'.')); run;
03-16-2018 03:11 PM