BookmarkSubscribeRSS Feed
whateverwhateve
Calcite | Level 5

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

3 REPLIES 3
Reeza
Super User

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

 

Tom
Super User Tom
Super User

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;
whateverwhateve
Calcite | Level 5
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.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 403 views
  • 4 likes
  • 3 in conversation