Programming the statistical procedures from SAS

sas date and time

Reply
Occasional Contributor
Posts: 6

sas date and time

Date
2013-07-05 00:10:52
2013-07-05 00:10:50
2013-07-05 00:10:49
2013-07-04 22:04:44
2013-07-04 22:04:42
2013-07-04 22:04:39
2013-07-04 22:04:36
2013-07-04 22:04:34
2013-07-04 22:04:10
2013-07-04 21:06:19

How to keep the rows with date and time after 2013-07-04 21:06:19 in SAS? Thanks!

Occasional Contributor
Posts: 6

Re: sas date and time

I don't know which SAS time format to use for this date and time column. Thank you!

Occasional Contributor
Posts: 6

Re: sas date and time

This column was  imported from Excel file, text only, $19..

Super Contributor
Posts: 307

Re: sas date and time

Ideally, you should convert the text values to actual date values. However, something like the following should work?

%let mydate = 2013-07-04 21:06:19;

Data want ;

     set your-file-name ;

     where date >= "&mydate";

run;

Super User
Posts: 1,074

Re: sas date and time

Hi

as you mentioned that the date time values are in text format(characters). It is important to convert them into numeric format to make them work in the if condition. and at the same time the date with which you are going to use as the condition (2013-07-04 21:06:19) should also be in numeric.

so please try the below code

data _null_;

    mydate=    input("2013-07-04 21:06:19",ANYDTDTM20.);

    call symput("mydate",mydate);

run;

%put &mydate;

the above code will create the macro variable which youare going to use as the condition.

the have dataset has the date times in text format, which i am converting into numeric format in another dataset, and passing the if condition.

data have;

    input date & $20.;

cards;

2013-07-05 00:10:52

2013-07-05 00:10:50

2013-07-05 00:10:49

2013-07-04 22:04:44

2013-07-04 22:04:42

2013-07-04 22:04:39

2013-07-04 22:04:36

2013-07-04 22:04:34

2013-07-04 22:04:10

2013-07-04 21:06:19

;

data want;

    set have;

    ndate=input(date,ANYDTDTM20.);

    format     ndate datetime20.;

    if &mydate < ndate ;

run;

Thanks,

Jagadish

Thanks,
Jag
Super Contributor
Posts: 307

Re: sas date and time

The text-based comparison should still work in this case, provided all of the dates have a consistent form (YYYY-MM-DD HH:MMSmiley FrustratedS) and the where clause contains a text value that is in the same form. But, I agree, a date conversion is preferable.

Super User
Posts: 1,074

Re: sas date and time

Yes, i believe text cannot be used to subset the condition especially when the subsetting condition is related to date time.

Thanks,
Jag
Super Contributor
Posts: 307

Re: sas date and time

The text comparison should work in this case (number-like). There are no letters that would mess it up (for example: JAN, AUG). The ASCII and EBCDIC collating sequences will both evaluate the comparison correctly, I believe.

Super Contributor
Posts: 307

Re: sas date and time

Are these numeric values (with a SAS format) or text?

Ask a Question
Discussion stats
  • 8 replies
  • 428 views
  • 0 likes
  • 3 in conversation