BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
K_S
Obsidian | Level 7 K_S
Obsidian | Level 7

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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.);

View solution in original post

10 REPLIES 10
Tom
Super User Tom
Super User

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.);
data_null__
Jade | Level 19

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. 

 

Screenshot 2021-09-08 132736.png

K_S
Obsidian | Level 7 K_S
Obsidian | Level 7

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

 

 

 

Reeza
Super User

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.

 

K_S
Obsidian | Level 7 K_S
Obsidian | Level 7

is the following more palatable?

 

new_char_var=INPUT(PUT(original_num_var, 8. -L), $CHAR40.);

 

 

Reeza
Super User
What does the log show?
K_S
Obsidian | Level 7 K_S
Obsidian | Level 7

the log is clean.. no error messages or warnings

Reeza
Super User
It's confusing though, just use the single PUT to get what you need, why the INPUT() again?
The INPUT doesn't do anything.

K_S
Obsidian | Level 7 K_S
Obsidian | Level 7

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?

 

 

Reeza
Super User

@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. 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 10 replies
  • 4173 views
  • 1 like
  • 4 in conversation