DATA Step, Macro, Functions and more

Counting number of date variables within given range

Posts: 0

Counting number of date variables within given range

I have a data set with many sas-date variables that represent dates on which certain events occured for each observation. If the event did not occur, the date variables are missing.

To count how many of the events occurred (and create a new variable with this information), I would simply use the N( var1, var2, varN...) function in a data step.

Now, if I need to count how many of these events occurred (i.e., how many variables have non-missing values) within a date range created by other variables, can I use the N function nested somehow in an if-then string? Is there another applicable function?

The data looks like this if my wording wasn't clear:

startdate enddate event1 event2 event3
4/12/98 5/15/98 4/14/98 5/26/98 4/30/98

And I need to find how many events happened between the startdate and enddate.

Super Contributor
Super Contributor
Posts: 3,174

Re: Counting number of date variables within given range

You have the INTCK function to consider. Also, you may want to consider a DO/END loop to check for dates within a low/high range -- consider that SAS numeric DATE variables are represented as "number of days since 1/1/1960".

Also, if all analysis is done within one observation, you can look at using your appropriate function, like N, and code this type of assignment statement:

34 data _null_;
35 input (startdate enddate event1 event2 event3) (mmddyy8.);
36 format startdate enddate event: date9.;
37 occur = n(of event: );
38 putlog _infile_ / _all_;
39 datalines;

4/12/98 5/15/98 4/14/98 5/26/98 4/30/98
startdate=12APR1998 enddate=15MAY1998 event1=14APR1998 event2=26MAY1998 event3=30APR1998 occur=3
_ERROR_=0 _INFILE_=4/12/98 5/15/98 4/14/98 5/26/98 4/30/98 _N_=1
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds

41 run;

Scott Barry
SBBWorks, Inc.
Posts: 0

Re: Counting number of date variables within given range

I finally buckled down and learned do loops. Thanks for the advice.

Ask a Question
Discussion stats
  • 2 replies
  • 2 in conversation