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.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 2347 views
  • 0 likes
  • 4 in conversation