How to use if-then with characters in SAS?

Reply
Occasional Contributor
Posts: 5

How to use if-then with characters in SAS?

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!

Super User
Posts: 11,343

Re: How to use if-then with characters in SAS?

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;

Occasional Contributor
Posts: 5

Re: How to use if-then with characters in SAS?

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.

Super Contributor
Posts: 1,636

Re: How to use if-then with characters in SAS?

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

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

Super User
Posts: 11,343

Re: How to use if-then with characters in SAS?

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.

Super Contributor
Posts: 349

Re: How to use if-then with characters in SAS?

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

Ask a Question
Discussion stats
  • 5 replies
  • 264 views
  • 0 likes
  • 4 in conversation