BookmarkSubscribeRSS Feed
Eugenio211
Quartz | Level 8

hello, hope all is well.

 

Can someone help in converting the example below to 'Date' in sas, thank you.

 

Character Date Value:

2023-02-16 03:06:32.9088697 -05:00

2 REPLIES 2
PaigeMiller
Diamond | Level 26

First, the fact that you have a character DATE/TIME value (not a character DATE value) indicates that somewhere earlier in the process, things were not being done in an optimal fashion. Calendar and clock information should always be numeric, never character. You might want to address this in your code (if you have created the character variable) or address this with the providers of the data if possible.

 

You asked to convert to 'DATE' and not date/time, so this will work:

 

data have;
    chardatetime='2023-02-16 03:06:32.9088697 -05:00';
run;
data want;
    set have;
    date=input(compress(chardatetime,'-'),yymmdd8.);
    format date yymmddd10.;
run;

 

 

--
Paige Miller
Tom
Super User Tom
Super User

Which DATE do you want?  The 16th as listed in the string?  Or do you want the 15th after you subtract the 5 hours implied by the negative 5:00 at the end?

 

And what do you want to do with the time of day component of the string?

You will probably want to parse the string into its three components and read each separately.

data have;
  input string $34.;
cards;
2023-02-16 03:06:32.9088697 -05:00
;

data want ;
  set have;
  date1 = input(scan(string,1,' '),yymmdd10.);
  tod = input(scan(string,2,' '),time16.);
  tzoffset = input(scan(string,3,' '),time6.);
  datetime1=dhms(date1,0,0,tod);
  datetime2=datetime1 + tzoffset;
  date2=datepart(datetime2);
  format date: yymmdd10. tod tod16.7 tzoffset time10. datetime: datetime26.5;
run;

Results:

1355  data _null_;
1356   set want;
1357   put (_all_) (=/);
1358  run;


string=2023-02-16 03:06:32.9088697 -05:00
date1=2023-02-16
tod=03:06:32.9088697
tzoffset=-5:00:00
datetime1=16FEB2023:03:06:32.90887
datetime2=15FEB2023:22:06:32.90887
date2=2023-02-15
NOTE: There were 1 observations read from the data set WORK.WANT.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 2 replies
  • 475 views
  • 0 likes
  • 3 in conversation