Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

counting years

Status
Not open for further replies.

jspizman

Technical User
Feb 18, 2006
1
US
Is there a way to count the number of times a specific date occurs between two dates. This is similar to the intck code but I want to know the number of times I cross Oct 01 and not Jan 01.

Thanks,
Josh
 
I can think of one possibility. Scale the date back so that you can use intck, ie, subtract the difference between 01OCT and 01JAN from both your dates and then use intck...

I don't know of a specific funtion to do this task, so I think it's going to have to be something like that.
 
You can try this logic:
Code:
%macro IntCheck(IDate1           = '01feb2001'd,
                IDate2           = today(),
                IntCheck         = 01/01,
                YourMacroVarName = NumberOfIntvls);
%global &YourMacroVarName;
data _null_;
  length
     int1 
     int2 $2;
  int1 = scan("&intcheck",1,'/');
  int2 = scan("&intcheck",1,'/');
  do Ix= &IDate1 to &IDate2;
    Iy = scan(put(Ix,mmddyy10.),1,'/');
    Iz = scan(put(Ix,mmddyy10.),2,'/');
   
    if trim(Iy) = trim(int1) and
       trim(Iz) = trim(int2) then
    ctr+1;
  end;
  call symput("&YourMacroVarName",trim(left(put(ctr,8.))) );   
run;
%mend IntCheck;
Basically the way this macro works is like this. It loops through all the days that exist between the two dates. Since a SAS date is a positive number between 1 (1/1/1960) and today we can use that number to loop through all date value between. You supply the start date and the end date, as well as the month/day that you are looking for. You also supply a macro variable name or use the default macro name of NumberOfIntvls. This runs in a datastep but I dont see why you couldn't run this in straight macro code if you needed to.
Hope that this helps you.
Klaz
 
My method didn't work when I tried it (not sure why). Not a fan of Klazs method as if you are looking at long periods it will slow your program down, especially if you're doing it for every record in a big dataset. However, I found something useful when looking at the doco for INTCK.

Try this code:-

Code:
data _null_;

  format date1 date2 ddmmyy10.;

  date1 = '01AUG2004'd;
  date2 = '15OCT2005'd;

  put date1=;
  put date2=;

  * Count number of 01JANs that appear in range *;
  jans = intck('YEAR',date1,date2);

  put jans=;

  * Count number of 01OCTs that appear in range *;
  octs= intck('YEAR.10',date1,date2);
  put octs=;
run;

The .10 "specifies an optional shift index that shifts the interval to start at a specified subperiod starting point. For example, YEAR.3 specifies yearly periods shifted to start on the first of March of each calendar year and to end in February of the following year."

A much nice way of doing it.
Check out the doco for other options on INTCK.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top