BookmarkSubscribeRSS Feed
mhamiltonsmith
Calcite | Level 5

I am trying to convert a date string written without hyphens into a date value. I can do so using the INPUT function if the date is written as "2001-01-01" by calling INPUT("2001-01-01", YYMMDD10.) However, I cannot do the same using YYMMDDN8. or any other format that does not include hyphens.

 

INPUT("2001-01-01", YYMMDD10.) = 14976  *Correct!

INPUT("20010101", YYMMDDN8.) = 20010101   *Incorrect

INPUT("2001.01.01", YYMMDDP8.) = .   *Incorrect

INPUT("2001 01 01", YYMMDDB8.) = .   *Incorrect

 

What am I doing wrong!?

 

Writing a date with hyphens is causing issues when I write a macro to convert a date string into a date value, so I cannot write the date string that way.

3 REPLIES 3
PaigeMiller
Diamond | Level 26

There is no informat YYMMDDN8. or YYMMDDP8. or YYMMDDB8.

 

data have;
    date_without_hyphens='20010101';
    date_with_hyphens='2001-01-01';
    date_with_periods='2001.01.01';
    date_without_hyphens2=input(date_without_hyphens,yymmdd8.);
    date_with_hypens2=input(date_with_hyphens,yymmdd10.);
    date_with_periods2=input(date_with_periods,yymmdd10.);
run;

  

--
Paige Miller
Patrick
Opal | Level 21

Informat YYMMDD10. works for all your date patterns.

data demo;
  infile datalines truncover;
  input dt_string $20.;
  format dt_sas date9.;
  dt_sas=input(dt_string,yymmdd10.);
  datalines;
2001-01-01
20010101
2001.01.01
2001 01 01
;

proc print data=demo;
run;

Patrick_0-1698277766908.png

 

 

Tom
Super User Tom
Super User

@Patrick wrote:

Informat YYMMDD10. works for all your date patterns.

data demo;
  infile datalines truncover;
  input dt_string $20.;
  format dt_sas date9.;
  dt_sas=input(dt_string,yymmdd10.);
  datalines;
2001-01-01
20010101
2001.01.01
2001 01 01
;

proc print data=demo;
run;

Patrick_0-1698277766908.png

 

 


Exactly.  The INPUT() function does not care if the WIDTH you used in the INFORMAT is larger than the LENGTH of the string you are reading.

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1883 views
  • 0 likes
  • 4 in conversation