SAS Data Integration Studio, DataFlux Data Management Studio, SAS/ACCESS, SAS Data Loader for Hadoop and others

filter the data by dates

Reply
Occasional Contributor
Posts: 10

filter the data by dates

[ Edited ]

I would like to choose some period in my data using the 2 dates.

I exported from each date year, month and day.

so here is my syntax but is doesn't work.

if (year=2016 and 4<month<=8) and (yearresult=2016 and 4<monthresult<=9 and dayresult=<15) then trimester='2016-2';

 date formats is ddmmyy10

 

May be there is another way to do the selection, but I couldn’t find it right now.

Thanks a lot for any help

 

Super User
Posts: 5,260

Re: filter the data by dates

What does not work?
Elaborate about your requirement. Perhaps using sample data and desired output.
Data never sleeps
Occasional Contributor
Posts: 10

Re: filter the data by dates

[ Edited ]

Thanks a lot for reactions.

 

So, I have to prepare a report for a period of time. I have 2 date variables: date_examination  and date_letter. date_letter is always some days later than date_examination.

Values date_examination are 10/05/2016 23/04/2016 and so on.

Values in date_letter are 15/05/2016 30/04/2016 and so on.

I have to choose all cases for date_letter between (01/05/2016 and 15/09/2016) and for date_examination between (01/05/2016 and 31/08/2016).

 

So I created the year, month, year variable from date_examination  and date_letter (see attachment). I use the following filter to do the selection:

if (yearresult=2016 and 4<monthresult<=9 and dayresult<=15) and (year=2016 and 4<month<=8)then trimester='2016-2';

 

Now, I have selected cases, but I am asking now may there is another way to select the period without creating the year, month and day variables?

 

Super User
Posts: 10,538

Re: filter the data by dates

You should describe how your code "doesn't work". Do you get errors? No result?Unexpected result?

With result issues then please provide some example values, the expected result and the actual result you are receiving.

 

From you comment it appears that you might have SAS date values.

So you may be intending to use the functions Year, Month and Day.

Which might mean you want something like

if (year(datevariable)=2016 and 4<month(datevariable)<=8) and (year(resultdatevariable)=2016 and 4<month(resultdatevariable)t<=9 and day(resultdatevariable)=<15) then trimester='2016-2';

 

But without seeing your variable names and what you are looking for exactly that is purely a guess.

I suspect that you might actually be looking for INTCK function to determine the interval between two dates.

Occasional Contributor
Posts: 10

Re: filter the data by dates

[ Edited ]

I didn't get any errors. But if I look at the data I have no values selected. I'll send some examples today.

Occasional Contributor
Posts: 10

Re: filter the data by dates

[ Edited ]

 

May be I can do like this?

if (2016/09/15>year,month,day(datevariable1)>=2016/05/01 and 2016/08/31>year,month,day(datevariable2)>=2016/05/01) then trimester='2016-2'; I'll try right now. Smiley Happy

 

Occasional Contributor
Posts: 10

Re: filter the data by dates

It doesn't work like this
Occasional Contributor
Posts: 10

Re: filter the data by dates

[ Edited ]

I tried another one:

format init_afspraakdatum ddMMMyyyy. DATUM_RESULTAAT_ARTS ddMMMyyyy.;

I've got

ERROR 48-59: The format DDMMMYYYY was not found or could not be loaded.

 

 

if (DATUM_RESULTAAT_ARTS >= "01MAY2016"d and DATUM_RESULTAAT_ARTS < "15SEP2016"d) and (init_afspraakdatum >= "01MAY2016"d and init_afspraakdatum <"31AUG2016"d )then trimester='2016-2';

run;

I've got

ERROR 180-322: Statement is not valid or it is used out of proper order.

Smiley Sad

 

Super User
Posts: 10,538

Re: filter the data by dates

It helps diagnose thing is you show the entire log with the errors. Copy and paste into the text box opened using the "run" icon at the top of the entry box here.

 

Some errors show up lines later when the cause is actually missing semicolon or mismatched quote several lines before the error.

Also the location of the underscore _ that appears in the log is helpful but the forum will not align that unless you use the code box opened with the run icon.

Occasional Contributor
Posts: 10

Re: filter the data by dates

Thanks ballardw,

 

You fint the entire log with the errors in the attachment. I don't get errors anymore to my selection

if "15SEP2016"d > DATUM_RESULTAAT_ARTS >="01MAY2016"d and "01AUG2016"d > init_afspraakdatum >="01MAY2016"d then trimester='2016-2';

 

But I still don't see the data selected (empty. I think I heva to change the format of my date variables.

Super User
Posts: 10,538

Re: filter the data by dates

The variable format will have no result on comparisons.

If you want the variable to display as 01JAN2016 the Format to use is DATE9.

If you want the variable to display as 21/01/2016 the format to use is  ddmmyy10.

Super User
Posts: 10,538

Re: filter the data by dates


sjughent wrote:

 

May be I can do like this?

if (2016/09/15>year,month,day(datevariable1)>=2016/05/01 and 2016/08/31>year,month,day(datevariable2)>=2016/05/01) then trimester='2016-2'; I'll try right now. Smiley Happy

 


If you want to specify a date literal the value must appear as 'ddMONyy'd with either single or double quotes: '15SEP2016'd, not 2016/09/15.  If you have an actual date variable and want to compare to two between 2 dates then

 

 ( '15SEP2016'd > datevaraible ge '01MAY2016'd) and ( '31AUG2016'd > datevariable2 ge '01MAY2016'd) then ....

 

The functions such as Year, Month or Day  would come into play if you were looking at something like the date was within a list of months or years such as:

   if month(date) in (4,5,6) then do 

to see if the month was April, May or June. 

Ask a Question
Discussion stats
  • 11 replies
  • 828 views
  • 1 like
  • 3 in conversation