BookmarkSubscribeRSS Feed
Torri92
Calcite | Level 5

Hi everyone,

I am trying to use an if-then statement with a variable character. I am running into a problem though where every time I run my code, the system converts my character variables into numeric variables and then defaults to the else statement. What I have is a multitude of characters from a large dataset. I only want to keep values that are 00, 26 or TC with everything else becoming 00. I initially tried the single quote since I thought that was used for characters, and then no quotes but both times everything has defaulted to 00. Here is the portion of the code that I am currently working on:

data rs0;

        set detail;

        if mod_1 = '00' then modifier = '00';

        else if mod_1 = '26' then modifier = '26';

        else if mod_1 = 'TC' then modifier = 'TC';

        else modifier = '00';

run;

proc print data=rs0 label;

        label

        modifier = "Modifier";

run;

There is some sorting and univariate commands between this, but I left the specific portion that I cannot get correct. If anyone could help me clear this up; it would be greatly appreciated. Thanks!

5 REPLIES 5
ballardw
Super User

You may need to run proc contents on your data set to see if the variables are actually numeric or not. Does MODIFIER already exist in the dataset as well?

Single or double quotes will work for delimiting strings.

Proc freq data=rs0; table mod_1;run; may be of interest. It could be that you actually don't have the values of 26 or TC in mod_1;

Torri92
Calcite | Level 5

I'm away from the server which has SAS loaded onto it but I'm almost certain that mod_1 is a character string. MODIFIER does NOT already exist in the dataset. My thought process was that the if-then statement would create a new variable so I gave it a unique name. There are a couple hundred codes which contain 26 or TC so I know that they do exist. I'll try the proc freq command when I get back to the server tomorrow. Thank you for your advice.

Linlin
Lapis Lazuli | Level 10

contain 26 or equal 26? there may be leading or trailing blacks. try:

if strip(mod_1) = '26' then modifier = '26';

ballardw
Super User

If the codes contain 26 or TC but have extra characters you may be looking for

if index(mod_1,'26')>0 then ...

instead of equals.

Especially if the mod_1 code looks like "000abc26-XYZ". In which case "=" is definitely not what you want.

shivas
Pyrite | Level 9

Hi,

To know if the variables contains trailing blanks try this...(borrowed from Tom's code) then you can change your if conditions accordingly.

data test;

ss='  05';output;

ss=' 09';output;

  run;

data _null_;

  set test;

  put (_character_) (= $quote. );

run;

Thanks,

Shiva

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
  • 5 replies
  • 1462 views
  • 0 likes
  • 4 in conversation