SAS documentation states that the source variable for input() function must be character.
I just tried the input function with a numeric source and it worked - i got what i wanted to get.
I had a numeric variable that i wanted to convert into a character variable that had a length of 40 so i did the following:
new_char_var=input(original_num_var, $char40.)
It worked but i wonder if this is bad practice and if yes, then why? What are the downsides of doing what I did.
Formats convert values to text. Informats convert text to values.
So if you want to convert your number to a character value use the PUT() function, not the INPUT() function.
When you used a numeric value as the first argument to the INPUT() function and a character informat as the second argument the SAS data step compiler noticed the conflict so it automatically converted the number into a character string (you will see messages about that in the SAS log). It will use the BEST12. format to do that. So you converted your number into a 12 character string (automatic conversion) and use the INPUT() function to convert that 12 character string into a 40 character string. You then assigned the value to the variable new_char. If NEW_CHAR was already defined then the value was either truncated or padded with spaces to fill its defined length. If NEW_CHAR had not yet been defined then the data step compiler would make a guess that you wanted to define it as length $40 to match the width of the informat you used in the INPUT() function call.
So you effective did:
length new_var $40;
new_char_var=put(original_num_var, best12.);
Formats convert values to text. Informats convert text to values.
So if you want to convert your number to a character value use the PUT() function, not the INPUT() function.
When you used a numeric value as the first argument to the INPUT() function and a character informat as the second argument the SAS data step compiler noticed the conflict so it automatically converted the number into a character string (you will see messages about that in the SAS log). It will use the BEST12. format to do that. So you converted your number into a 12 character string (automatic conversion) and use the INPUT() function to convert that 12 character string into a 40 character string. You then assigned the value to the variable new_char. If NEW_CHAR was already defined then the value was either truncated or padded with spaces to fill its defined length. If NEW_CHAR had not yet been defined then the data step compiler would make a guess that you wanted to define it as length $40 to match the width of the informat you used in the INPUT() function call.
So you effective did:
length new_var $40;
new_char_var=put(original_num_var, best12.);
Did you notice the log message? The number is converted to character then read with $CHAR which preserves leading spaces. In general it makes you look like you don't know what you're doing. There may be a scenario where it is exactly what you want to do but I can't think of one.
the proper way of doing this is define the length of 40 at the beginning and then do
data out;
set in;
length new_char_var $40.;
new_char_var=put(original_num_var, 8. -L);
run;
?
by doing new_char_var=compress(input(original_num_var, $char40.)); i get the exact same output
Because SAS is fixing it for you behind the scenes, which is why the log is different if you do it correctly versus doing it the original way.
is the following more palatable?
new_char_var=INPUT(PUT(original_num_var, 8. -L), $CHAR40.);
the log is clean.. no error messages or warnings
the length needs to be 40 and i want to avoid specifying the length in a separate line. So converting numeric with lenght 8 into character with length 8 and then applying $char40. to make the length 40.
is there a way to do that with a single put?
@K_S wrote:
the length needs to be 40 and i want to avoid specifying the length in a separate line. So converting numeric with lenght 8 into character with length 8 and then applying $char40. to make the length 40.
is there a way to do that with a single put?
That's an artificial requirement that really makes no sense. It means you start breaking the typical coding structure and your code is unclear but if that's what you want go for it.
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.