'input' can return a character variable if both the input variable and format are characters. But a 'put' always returns a character. See codes below:
data two;
age=40;
char1='10';
char2='$1,000.00';
char3=input(char1, $4.);
num4=input(char2, dollar10.2);
char5=put(age, 4.);
run;
Is there a question here?
An INFORMAT translates text into values. A FORMAT translates values to text. A numeric informat creates numeric values. A numeric format works on numeric values.
To convert from text to numbers you must use an INFORMAT.
To convert from numbers to text you must use a FORMAT.
To convert from text to text you can use either.
To convert from numbers to numbers just use an expression. But you could use a format to convert the number to text and then use an informat to convert the generated text back to a number.
char3=input(char1, $4.);
num4=input(char2, dollar10.2);
char5=put(age, 4.);
the question is how to define 'informat' and 'format'' in these converts. According to the definition of 'informat' (tells SAS how to read the variables) all of these formats '$4., dollar 10.2 and 4.' are informats. Then how to understand 'format' in your words:
A FORMAT translates values to text.
To convert from numbers to text you must use a FORMAT.
To convert from text to text you can use either.
what are details/principles of 'input'/'put' in terms of 'format/informat'?
@anming wrote:
According to the definition of 'informat' (tells SAS how to read the variables) all of these formats '$4., dollar 10.2 and 4.' are informats.
Wrong. $4., DOLLAR10.2 are INformats because they are used in the INPUT function, 4. is a format because it is used in a PUT function.
I still don't understand what you are asking. In this code:
char3=input(char1, $4.);
num4=input(char2, dollar10.2);
char5=put(age, 4.);
You are using 2 informats and 1 format.
The results of input(char1, $4.) is the same as you would get with left(substrn(char1,1,4)).
This function call, input(char2, dollar10.2) , says to read the first 10 bytes of CHAR2 and interpret it as a number after removing any dollar signs, commas, or percent signs from the 10 bytes. And if the string does not have a period to mark the decimal place then assume the last two digits in the string are after the decimal place.
This function call, put(age, 4.) , will create a string of four characters. The digits will be right aligned in the four characters (padded in front with spaces). So if AGE=40 the result will be two spaces followed by the digit 4 and the digit 0.
You want to be very careful specifying decimals in an informat.
Consider:
data two; char2='$1,000'; num4=input(char2, dollar10.2); run;
What do you think the value for Num4 should be? What is it when you look at the actual data?
This example shows you don't need the decimal:
data three; char2='$1,000.25'; num4=input(char2, dollar10.); run;
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.