PUT and INPUT

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 19
Accepted Solution

PUT and INPUT

Hi All.

 

I am new to SAS and started exploring.

I am bit confused with using PUT and INPUT, please help me.

 

I have a column named Birth_date of type numeric and length 8 Format and Informat MMDDYY10.

 

Q1.I am running the below code snippets to get the b_date from Birth_Date 
 
data birth;
set credit card;
b_date= input(Birth_Date,10.);
format b_date date9.
run;
 
Why for Birth_Date 01/08/1966, b_date = 22JAN1960 ?
 
Q2. Running the below code Snippets.
 
data birth;
set credit card;
b_date= input(Birth_Date,10.);
run:
 
Why for Birth_Date 01/08/1966,  b_date = 21?
 
Q3) Running the below code Snippets.
 
data birth;
set credit card;
b_date= put(Birth_Date,10.);
run:
 
Why for Birth_Date 01/08/1966,  b_date = 2199?
 
 

Accepted Solutions
Solution
‎03-29-2017 12:15 PM
Super User
Posts: 5,362

Re: PUT and INPUT

When SAS is instructed to perform an operation on a character string, but the value fed to that operation is numeric, SAS has to perform a numeric-to-character conversion.

 

Most of the time, SAS generates a 12-character-long string.  So if the numeric value coming in is 3.14, SAS converts this to 8 blanks, followed by the characters "3.14".  The conversion process right-hand justifies the original numeric value within the 12-character-long string.

View solution in original post


All Replies
Community Manager
Posts: 2,889

Re: PUT and INPUT

Super User
Posts: 19,105

Re: PUT and INPUT

Birth date is already a SAS date. 

Insread of PUT/INPUT apply a FORMAT statememt. 

 

Format birth_date DATE9.;

 

. This section of the SAS documentation covers SAS dates quite thoroughly. 

 

http://support.sas.com/documentation/cdl/en/basess/68381/HTML/default/viewer.htm#p1638plsagruv4n1bia...

Super User
Posts: 5,362

Re: PUT and INPUT

To answer the "why" questions, you have to begin by understanding how SAS stores dates.  That number (2199) means that January 8, 1966 falls 2199 days after January 1, 1960.  That's how SAS expects to store dates, and why Reeza's reply will give you part of the answer to your questions.

 

The other part of the "why" questions concerns the INPUT function.  INPUT expects to see a character string as the first argument, and reads that character string using the instructions within the second argument.  Here, you are passing a number as the first argument.  That forces SAS to perform a numeric-to-character conversion to be able to apply the INPUT function.  (You might have noticed a message about conversion in the log.)  And when SAS performs a numeric-to-character conversion, the usual method it applies is to use a 12-character field, right-hand justified.  So the INPUT function is actually reading 8 blanks, followed by "2199".  If you apply instructions that tell the INPUT function to read only 10 characters, it finds 8 blanks plus "21".

Occasional Contributor
Posts: 19

Re: PUT and INPUT

Hi 

 

Thanks for replying, it really helps me a lot.

 

Could you please explain the below statement, i could not able to get this.

 

"And when SAS performs a numeric-to-character conversion, the usual method it applies is to use a 12-character field, right-hand justified.  So the INPUT function is actually reading 8 blanks, followed by "2199".  If you apply instructions that tell the INPUT function to read only 10 characters, it finds 8 blanks plus "21".

 

Thanks & Regards 

Vishyy

Solution
‎03-29-2017 12:15 PM
Super User
Posts: 5,362

Re: PUT and INPUT

When SAS is instructed to perform an operation on a character string, but the value fed to that operation is numeric, SAS has to perform a numeric-to-character conversion.

 

Most of the time, SAS generates a 12-character-long string.  So if the numeric value coming in is 3.14, SAS converts this to 8 blanks, followed by the characters "3.14".  The conversion process right-hand justifies the original numeric value within the 12-character-long string.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 265 views
  • 5 likes
  • 4 in conversation