DATA Step, Macro, Functions and more

Date format

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 15
Accepted Solution

Date format

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-31-2017 01:26 AM
Super User
Posts: 10,500

Re: Date format

Adding to @ccarel

 

The value of Birth_date apparently is already a SAS date value. Run proc contents on your data set and I bet you see Birth_date has Format mmddyy10.

 

What you did with this code:

b_date= input(Birth_date,10.);

was ask SAS to convert the existing value of Birth_date to a string value that the INPUT function requires. The unformatted value of 08Jan1966 is 2199. When SAS does the requested conversion it uses the best12. format so the resulting string 12 characters long with a value of 8 blanks followed by 2199. Then you tell SAS to read that value with 10. format so it reads the first 10 characters. The only numerals there are 21 so the result is 21. When you format that as a date you get 22 Jan 1960 because 0 is 1 Jan 1960 and dates are number of days from 1 Jan 1960. So add 21 = 22 Jan 1960.

 

If your value had actually been the string '01/08/1966' there would have been an error because that is not valid to read with the 10. informat.

 

Your log when you ran the code would have message about "Numeric values have been converted to character values at the places given by :

(Line)Smiley SadColumn).

the line and column shown would point to the location of Birth_date in the input function argument.

 

 

View solution in original post


All Replies
PROC Star
Posts: 551

Re: Date format

Can you provide some sample data?

Occasional Contributor
Posts: 15

Re: Date format

can you please try with the date which has been given in the question 

Super User
Posts: 6,936

Re: Date format

You need to give us a sample of your datasets credit and card (as yout set statement adresses those two datasets).

We need this to see the actual values and the variable types and attributes.

Provide the data in a data step; a macro that converts your dataset to a data step for posting is found here:

https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat...

When posting, use the "little running man" or {i} icons. These preserve formatting and prevent reformatting of supposed smileys etc.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Occasional Contributor
Posts: 15

Re: Date format

PFA for sample data set

Attachment
Super User
Posts: 6,936

Re: Date format

[ Edited ]

The problem is that a csv file does not convey column attributes in any way, which are essential in getting the right answers.

Or do you mean that you do not yet have a SAS dataset and need proper import code for the csv also?

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Occasional Contributor
Posts: 15

Re: Date format

The data set i have importeed correctly but I am not understanding why I am getting different out put while using put and input 

please help me on that

Super User
Posts: 6,936

Re: Date format

Show the code with which you imported the csv, so we can recreate your dataset.

Or use the macro I previously mentioned to convert your dataset into a data step for posting here.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Posts: 5,082

Re: Date format

Board monitors,

 

Perhaps it's time to compare this to:

 

https://communities.sas.com/t5/General-SAS-Programming/PUT-and-INPUT/m-p/345080#M44559https://commun...

 

Looks like there's a homework assignment here.

Super User
Posts: 6,936

Re: Date format

Oh, yeah, some people need to get a grasp on the concept that homework assignments want to test THEIR brains, not OURS.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Contributor
Posts: 20

Re: Date format

 
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 ?
 
Answer: The b_date is 22JAN1960 b/c that is the DATE9. format that you requested in the code.  You also need a semicolon (Smiley Wink after DATE9.
 
 
Solution
‎03-31-2017 01:26 AM
Super User
Posts: 10,500

Re: Date format

Adding to @ccarel

 

The value of Birth_date apparently is already a SAS date value. Run proc contents on your data set and I bet you see Birth_date has Format mmddyy10.

 

What you did with this code:

b_date= input(Birth_date,10.);

was ask SAS to convert the existing value of Birth_date to a string value that the INPUT function requires. The unformatted value of 08Jan1966 is 2199. When SAS does the requested conversion it uses the best12. format so the resulting string 12 characters long with a value of 8 blanks followed by 2199. Then you tell SAS to read that value with 10. format so it reads the first 10 characters. The only numerals there are 21 so the result is 21. When you format that as a date you get 22 Jan 1960 because 0 is 1 Jan 1960 and dates are number of days from 1 Jan 1960. So add 21 = 22 Jan 1960.

 

If your value had actually been the string '01/08/1966' there would have been an error because that is not valid to read with the 10. informat.

 

Your log when you ran the code would have message about "Numeric values have been converted to character values at the places given by :

(Line)Smiley SadColumn).

the line and column shown would point to the location of Birth_date in the input function argument.

 

 

☑ This topic is SOLVED.

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

Discussion stats
  • 11 replies
  • 242 views
  • 1 like
  • 6 in conversation