Hello
I want to check type of var and if it numeric then convert to char and if it char then don't change it.
Please see 2 examples.
In example 2 there is an error,
what is the way to solve it please?
/**Example1-Work well**/
Data ttt1;
input x $ y ;
cards;
a 10
b 20
;
run;
Data want1;
set ttt1;
type_Y=vtype(Y);
IF type_Y='N' then Y_new=put(Y,best.);
else Y_new=Y;
Run;
/**Example2-Doesnt Work well**/
/*37 IF type_Y='N' then Y_new=put(Y,best.);*/
/* _____*/
/* 484*/
Data ttt2;
input x $ y $;
cards;
a 10
b 20
;
run;
Data want2;
set ttt2;
type_Y=vtype(Y);
IF type_Y='N' then Y_new=put(Y,best.);
else Y_new=Y;
Run;
You PUT a character variable, so the numeric BEST format cannot be used.
The error message is clear. You tried to use a numeric format with a character variable.
But you also have a logic problem with your code.
The compiler checks all of the code for syntax errors, even the code that might never be executed.
Since you never defined Y_NEW the compiler will use the first place it sees it being used to GUESS how you want it defined. Since the first place is the assignment of the value of the PUT() function then Y_NEW will always be a CHARACTER variable.
In general you should be doing this in two steps. One to determine the variable type. Then conditionally generate the code based on the type. For example by creating a macro variable and using it to generate the code in the second step.
But for the simple case of converting both CHAR and NUM variables to CHAR you can just write code that doesn't care what type the original variable was. Just use the CATS() function.
data want;
set have;
length new_y $50;
new_y=cats(y);
rename new_y=y y=old_y;
run;
Note there is a side effect hat any leading spaces in a character variable Y will be removed.
Hello
As I understand CATS,CATX are char functions?
As I understand It means that the outcome of these functions is a char variable.
I was sure that the input of these functions must be char too.
In other question in this forum it was told me that I can also use numeric variables (as inputs of CATS,CATX functions ).
May anyone explain it and verify that can use numeric variables?
CATS and CATX can have numeric values as input. You can verify it yourself.
What about compress? Can it also get input numeric var?
Can all char functions get input numeric values?
The SAS documentation spells all this out.
specifies a constant, variable, or expression, either character or numeric. If item is numeric, its value is converted to a character string by using the BESTw. format. In this case, SAS does not write a note to the log
specifies a character constant, variable, or expression from which specified characters are removed.
Because SAS (the data step compiler) will automatically convert types of values wherever needed, functions that expect character values will work with numeric variables, but often with unexpected and sometime hilarious effects. Serious SAS users will not let that happen and do the conversion explicitly.
The deeper question here: why is there a need to convert data types in the first place? Instead of repairing issues after the fact, you need to look at the process where the variables originate, and do the fix there.
@Ronein wrote:
What about compress? Can it also get input numeric var?
Can all char functions get input numeric values?
You can easily find that out yourself. Maxim 4 says
So run this, and study the log closely:
data _null_;
x1 = 12345;
x2 = compress(x1);
put x2=;
run;
You need to be aware that any function that will create a character result from a numeric value will use SAS default rules if you do not take control of the conversion. Control usually means a format and possibly a length or justification.
Many of the conversions will use a BEST12 format resulting in leading spaces, or in the case of "large" values exponential notation and/or truncation to fit the number of characters.
Here's a workaround to a tricky problem:
Data _null_;
set ttt2;
call execute('data want2; set tt2;');
type_Y=vtype(Y);
if type_Y='N' then call execute('Y_new=put(Y,best.);');
else call execute('Y_new=Y;');
call execute('run;');
stop;
run;
It's not clear that "best." is a good format choice. If that turns out to be the case, you will need to inspect the results, and perhaps post another question.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.