BookmarkSubscribeRSS Feed
balukumar
Calcite | Level 5

Hi I have one doubt in sas
in dataset date format have 08-04-1988
base on this values I want create date styles and along flag values
For datetime flag creation if dateandtime have then consider flag 1 else 0
for date if date have then consider flag 1 else 0
for time if time have then consider flag 1 else 0


DATA DAT;
X= "08APR1988"D;
FORMAT X DDMMYYD10.;
RUN;

Based on above value I want output like below

Datetime | Date | Time | DatetimeFlag | DateFlag |TimeFlag
08APR1988 00:00:00 |08-04-1988 |00:00:00 | 0 |1 |0

I tried like below
DATA DUMM;
SET DAT;
DT=PUT( X, DDMMYY10.);
DATEF=PUT( X, date9.);
DTs=PUT( X, DATETIME32.);
TimeF=PUT( X, TIME.);
RUN;

here if donot have time then its takeing default datewith time similar time also
i want display if time is not available then consider 00:00:00 similar if date not available the date aslo : 1960-01-01

but above scirpt is not given expeted result ,
can you please tell me how to wirte sas coding to achive this task.

3 REPLIES 3
Tom
Super User Tom
Super User

You don't seem to understand how dates and times work.

Dates are numbers of days. Datetime (and time) are numbers of seconds. If you just apply a datetime format to a value that is in days it will end up looking like some time early on the day 01JAN1960 since there are 86,000 seconds in a day.

 

You can use the DATEPART() and TIMEPART() function to extract the number of days or the seconds since midnight from a datetime value.

You can build a datatime value from a DATE value by using the DHMS() function. It takes 4 arguments,Days, Hours, MInutes, Seconds, but you really only need to give it the DAYS and SECONDS since you can give a number of seconds that is larger than 60.

 

7    data test ;
8      date="08APR1988"D;
9      datetime=dhms(date,0,0,0);
10     put 'RAW Values ' (date datetime) (= comma15.);
11     put 'Formatted Values ' date= date9. datetime= datetime20. ;
12   run;

RAW Values date=10,325 datetime=892,080,000
Formatted Values date=08APR1988 datetime=08APR1988:00:00:00
13   data test ;
14     date="08APR1988"D;
15     datetime=dhms(date,0,0,'08:20't);
16     put 'RAW Values ' (date datetime) (= comma15.);
17     put 'Formatted Values ' date= date9. datetime= datetime20. ;
18   run;

RAW Values date=10,325 datetime=892,110,000
Formatted Values date=08APR1988 datetime=08APR1988:08:20:00

 

Tom
Super User Tom
Super User

What is your actual input?  The trivial example input you show just has the one DATE value.  So by definition it does not (could not) have any time component.

 

Are you starting with a string that you want to convert into date, time and datetime values?

Reeza
Super User

Welcome to the SAS Community Forums.

 

This post is very unclear.

Please try and clearly post the following information:

 

1. What data do you have?

Here are instructions on how to provide sample data as a data step:
https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat...

 

2. What do you want? Based on the input data post what you expect as output. 

 

3. What is the logic to get there and what have you tried so far that didn't work?

 

PUT() converts variables so will not create any flags. As your pseudocode indicates, you likely need IF/THEN statements. 

 

 

Here's a great, but longer and in depth, reference for dates and times in SAS
https://communities.sas.com/t5/SAS-Communities-Library/Working-with-Dates-and-Times-in-SAS-Tutorial/...

 

 

Vague questions will get vague answers and taking some time to clarify your question will help you get faster and relevant answers. 

 


@balukumar wrote:

Hi I have one doubt in sas
in dataset date format have 08-04-1988
base on this values I want create date styles and along flag values
For datetime flag creation if dateandtime have then consider flag 1 else 0
for date if date have then consider flag 1 else 0
for time if time have then consider flag 1 else 0


DATA DAT;
X= "08APR1988"D;
FORMAT X DDMMYYD10.;
RUN;

Based on above value I want output like below

Datetime | Date | Time | DatetimeFlag | DateFlag |TimeFlag
08APR1988 00:00:00 |08-04-1988 |00:00:00 | 0 |1 |0

I tried like below
DATA DUMM;
SET DAT;
DT=PUT( X, DDMMYY10.);
DATEF=PUT( X, date9.);
DTs=PUT( X, DATETIME32.);
TimeF=PUT( X, TIME.);
RUN;

here if donot have time then its takeing default datewith time similar time also
i want display if time is not available then consider 00:00:00 similar if date not available the date aslo : 1960-01-01

but above scirpt is not given expeted result ,
can you please tell me how to wirte sas coding to achive this task.


 

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

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
  • 957 views
  • 0 likes
  • 3 in conversation