BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
salvavit
Fluorite | Level 6

Hi all, I have a list of Datetime values (e.g. 02JAN1970:01:00:02) that I want to convert into numerical having the following format: 19700102

I have wrote the following code, but it ends up showing a list of dots instead of the actual dates.

data table_2;
set table_1;
day_p = datepart(day_p);
format day_p yymmddn8.;
run;

data table_3; 
set table_2; 
day_p = input(put(input(day_p,date9.),yymmddn8.),8.);
format day_p 8.;
run;

May anyone help me in solving this issue?

Thank you in advance

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

You have an INPUT() too many:

data want;
dt = "02JAN1970:01:00:02"dt; 
day_p = datepart(dt);
day_n = input(put(day_p,yymmddn8.),8.);
format
  dt datetime19.
  day_p yymmddd10.
  day_n 8.
;
run;

Be advised that the result is not useful for anything. You can't use it in calculations, cannot display it nicely, and can't use the gazillion goodies that SAS provides for handling dates. It needs more bytes than a SAS date (which can be stored with 4) on top.

View solution in original post

3 REPLIES 3
Kurt_Bremser
Super User

You have an INPUT() too many:

data want;
dt = "02JAN1970:01:00:02"dt; 
day_p = datepart(dt);
day_n = input(put(day_p,yymmddn8.),8.);
format
  dt datetime19.
  day_p yymmddd10.
  day_n 8.
;
run;

Be advised that the result is not useful for anything. You can't use it in calculations, cannot display it nicely, and can't use the gazillion goodies that SAS provides for handling dates. It needs more bytes than a SAS date (which can be stored with 4) on top.

salvavit
Fluorite | Level 6
Hi Kurt, Thanks for your help and suggestions. So what should I do if
I need to compare it to another date which is stored in the same
format (19700201)?
If I have understood correctly, your solution is to convert both dates
into SAS dates and then compare them?
Kurt_Bremser
Super User

Absolutely. Anything that is a date, time or datetime value should be stored as such in SAS. Even things that show only periods (year/month) should be stored as a date (1st of month) with a format that shows year/month only, like yymmd7. Because then you can still use functions to calculate intervals etc.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!

Submit your idea!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 626 views
  • 0 likes
  • 2 in conversation