DATA Step, Macro, Functions and more

What am I doing wrong as Date output shows not proper.

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 15
Accepted Solution

What am I doing wrong as Date output shows not proper.

Hi,

 

Below code when executed gives

**********

data ds_dtcon;
mydt='01/01/01';
mynumdt = input(mydt,ddmmyy10.);
format mynumdt ddmmyy10.;
run;

**********

Output

mydt = 01/01/01

mynumdt = 01/01/2001

**********

 

But the same code as shown below when executed with different date pattern it shows wrong date output.

data ds_dtcon1;
mydt='01/jan/2001';
mynumdt = input(mydt,date9.);
format mynumdt date9.;
run;

**********

Output

mydt = 01/jan/2001

mynumdt = 01JAN2020

**********

 

Any guidance is appreciated.

Thanks


Accepted Solutions
Solution
‎12-10-2015 09:55 AM
Super User
Posts: 5,082

Re: What am I doing wrong as Date output shows not proper.

The problem lies here:

 

mynumdt = input(mydt, date9.);

 

The instruction "date9." tells SAS to read only 9 characters of MYDT.  It isn't reading the last two characters.  Switch to:

 

mynumdt = input(mydt, date11.);

 

Good luck.

View solution in original post


All Replies
Solution
‎12-10-2015 09:55 AM
Super User
Posts: 5,082

Re: What am I doing wrong as Date output shows not proper.

The problem lies here:

 

mynumdt = input(mydt, date9.);

 

The instruction "date9." tells SAS to read only 9 characters of MYDT.  It isn't reading the last two characters.  Switch to:

 

mynumdt = input(mydt, date11.);

 

Good luck.

Occasional Contributor
Posts: 15

Re: What am I doing wrong as Date output shows not proper.

Thanks.

Occasional Contributor
Posts: 15

Re: What am I doing wrong as Date output shows not proper.

I got confused because of below code  where for Informat mydt date7. is mentioned and still it gives proper output.

 

data ds_conv;
input mydt;
informat mydt date7.;
format mydt date9.;
datalines;
10-Jan-2014
10-dec-2003
;
run;

 

Super User
Posts: 10,500

Re: What am I doing wrong as Date output shows not proper.

Input behaves differently with assigned informat, your example code, and informat in line.

Run this code and see what happens:

data work.ds_conv;
format mydt date9.;
input mydt date7.;
datalines;
10-Jan-2014
10-dec-2003
;
run;

Your original use of the input function with wrong value is like the above code not like the example that worked.

 

Occasional Contributor
Posts: 15

Re: What am I doing wrong as Date output shows not proper.

Thanks BallardW for the knowledge sharing and reply. The output shows ' . ' (period) incase of inline informat. I am newbie and learning, if possible can you share any links to read more on these topic.

Trusted Advisor
Posts: 1,115

Re: What am I doing wrong as Date output shows not proper.

Regarding the INPUT statement (as opposed to the INPUT function), I think it's most important to know the differences between list input and formatted input, which are two of the four different styles of reading raw data with the INPUT statement (the other two are called column input and named input, the latter being only rarely used, at least in my experience). These are described in the online documentation on the INPUT statement and in more detailed pages which are linked there.

The INFORMAT Statement can be used to perform so called modified list input (but there is another, I think more frequently used way to do this, see the above documentation on list input). This is explained in section "How SAS Treats Variables when You Assign Informats with the INFORMAT Statement" of the documentation on the INFORMAT Statement. There you find the explanation why you were able to read dates such as 10-Jan-2014 with the "too short" date7. informat in the INFORMAT statement: The width 7 is simply ignored in this case! (See third bullet point of that section.)

 

The INPUT function shares some features with formatted input (see above). In particular, specifying an informat with a sufficient length is important.

 

☑ This topic is SOLVED.

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

Discussion stats
  • 6 replies
  • 294 views
  • 0 likes
  • 4 in conversation