07-25-2012 04:27 PM
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:
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!
07-25-2012 05:02 PM
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;
07-25-2012 05:45 PM
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.
07-25-2012 06:50 PM
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.
07-26-2012 05:09 AM
To know if the variables contains trailing blanks try this...(borrowed from Tom's code) then you can change your if conditions accordingly.
put (_character_) (= $quote. );