BookmarkSubscribeRSS Feed
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';


proc print data=rs0 label;


        modifier = "Modifier";


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!

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;

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.

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';

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.

Pyrite | Level 9


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;


data _null_;

  set test;

  put (_character_) (= $quote. );




Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 4 in conversation