Hi, I'm sorry to repeat same topic, but i couldn't find in existing "IF-THEN" posted discussions to match my trouble.
I have can't figure out very simple scenario with "IF-THEN" statement and log issue "Numeric values have been converted to character values":
Here is my code:
data have;
input type xxtype $ oldvar;
cards;
1 num 0
1 num 0
1 num 22
1 num 5
1 num 0
1 num 10
;
run;
data want;
set have;
length check newvar $200;
if xxtype="num" then do; check="check1: "||strip(put(type,best12.)); newvar=strip(put(oldvar,best12.)); end;
else if xxtype="char" then do;
put "type is character=" type;
check="check2: "||strip(put(type,best12.));
newvar=strip(oldvar);
end;
run;
Here is log issue:
NOTE: Numeric values have been converted to character values at the places given by: (Line):(Column).
ELSE statement should never execute, nevertheless it seem SAS is tripping over the newvar=strip(oldvar); line.
If I missed very obvious logical fault, I apologize for my silly question.
You cannot treat a variable as both numeric and character in the same data step. You might consider using macro code to only generate the right type of statements, but that is not needed if you use either CATS() or VVALUE().
%let myvar=oldvar;
data want;
set have;
length newvar $200 ;
newvar=vvalue(&myvar);
run;
VVALUE() will give you the formatted value and CATS() will give you the raw value.
That is a one time note of SAS saying that there is an application of a character function to a numeric variable as SAS realises that oldvar is a numeric variable based on input.
One issue is that you created XXTYPE in the first data step and are referencing TYPE in the second data step.
Another issue is that you are calling the character function STRIP() put giving it a numeric variable as the input.
Are you trying to convert the numeric variable to character? What format do you want to use to convert the number into text?
You can use CATS() to automatically convert the numeric value into a string. It will use BEST32 format instead of the BEST12 format.
You can also use VVALUE() function to get the formatted value of the variable.
You cannot treat a variable as both numeric and character in the same data step. You might consider using macro code to only generate the right type of statements, but that is not needed if you use either CATS() or VVALUE().
%let myvar=oldvar;
data want;
set have;
length newvar $200 ;
newvar=vvalue(&myvar);
run;
VVALUE() will give you the formatted value and CATS() will give you the raw value.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.