Appying two formats to a variable based on another variable

Reply
New Contributor
Posts: 2

Appying two formats to a variable based on another variable

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

Thanks,

Kelli

Super User
Posts: 23,754

Re: Appying two formats to a variable based on another variable

Posted in reply to whateverwhateve

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’);

 

Super User
Super User
Posts: 8,115

Re: Appying two formats to a variable based on another variable

Posted in reply to whateverwhateve

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;
New Contributor
Posts: 2

Re: Appying two formats to a variable based on another variable

Posted in reply to whateverwhateve
Thanks Reeza & Tom for your quick replies! I figured I was going to have to create another variable and you confirmed that for me. I very much appreciate your examples. That is exactly what I'll use. Thanks again.
Ask a Question
Discussion stats
  • 3 replies
  • 141 views
  • 4 likes
  • 3 in conversation