BookmarkSubscribeRSS Feed
sjughent
Obsidian | Level 7

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

 

11 REPLIES 11
LinusH
Tourmaline | Level 20
What does not work?
Elaborate about your requirement. Perhaps using sample data and desired output.
Data never sleeps
sjughent
Obsidian | Level 7

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?

 

ballardw
Super User

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.

sjughent
Obsidian | Level 7

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

sjughent
Obsidian | Level 7

 

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. 🙂

 

sjughent
Obsidian | Level 7
It doesn't work like this
sjughent
Obsidian | Level 7

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.

😞

 

ballardw
Super User

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.

sjughent
Obsidian | Level 7

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.

ballardw
Super User

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.

ballardw
Super User

@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. 🙂

 


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. 

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 connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 11 replies
  • 6963 views
  • 1 like
  • 3 in conversation