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

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?
 
 
1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

5 REPLIES 5
ChrisHemedinger
Community Manager

Super User @MichelleHomes has shared a really useful blog post about PUT and INPUT and how to remember the difference.

It's time to register for SAS Innovate! Join your SAS user peers in Las Vegas on April 16-19 2024.
Reeza
Super User

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

Astounding
PROC Star

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

vishyy
Obsidian | Level 7

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

Astounding
PROC Star

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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 7608 views
  • 7 likes
  • 4 in conversation