BookmarkSubscribeRSS Feed
anming
Pyrite | Level 9

'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;

 

5 REPLIES 5
Tom
Super User Tom
Super User

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.

 

anming
Pyrite | Level 9
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'?

 

Kurt_Bremser
Super User

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

Tom
Super User Tom
Super User

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.

 

ballardw
Super User

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;

 

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!

Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 5 replies
  • 951 views
  • 3 likes
  • 4 in conversation