BookmarkSubscribeRSS Feed
Fluorite | Level 6



I am trying to code a macro to get a column as a time period, which is called wantdate,  It is accumulating till today every day. For example, if the startdate=01/01/2016, and enddate=02/27/2016. The wandate should cover the data as wantdate1, which is 01/01/2016, wantdate2, which is betweem 01/01/2016 and 01/02/2016, wantdate3, which is between 01/01/2016 and 01/03/2016,wantdate4, which is between 01/01/2016 and 01/04/2016.....and the last one is wantdate57, which is   between 01/01/2016 and 02/27/2016. Is the code below good? If not, how to correct it? Thanks a lot!


%let startdate = 01jan2016;

%let enddate = 27feb2016;

*Number of loops;

%let iterations = %sysfunc(intck(day, "&startdate."d, "&enddate."d));


*Initialize new date variables four counting;

%let lowerdate = &startdate;

%let upperdate = %sysfunc(intnx(month, "&lowerdate"d, 11, end), date9.);

%put &lowerdate.;

%put &upperdate.;

%put &iterations;

%macro cases_per_year;

%do i = 1 %to &iterations;

/*    proc sql noprint;*/

/*    select count(zip)*/

/*    into :nr_obs*/

/*    from cases*/

/*    where (diagdate GE "&lowerdate"d AND diagdate LE "&upperdate"d);*/

/*    run;*/

/*    quit;*/

    *Increment both the lower and the upper boundary of the time interval by one year for the next iteration to count the numbers of the following year and so on;

    %let lowerdate = %sysfunc( intnx(year, "&lowerdate"d, 1, beginning), date9.);

    %let upperdate = %sysfunc(intnx(year, "&upperdate"d, 1, end), date9.);


%put &lowerdate.;

%put &upperdate.;


%mend cases_per_year;


Super User

I don't think you need a macro and it's probably making your code too complex.

Can you post sample input data and sample output and explain your logic.


Also, do you have SAS/ETS?

Super User

Since it appears that your data has a date variable then perhaps you would want to look at this:


Proc freq data=cases;

   tables diagdate;

   format diagdate yearr4.;



Or possibly


Proc freq data=cases;

   tables diagnosisvariable * diagdate;

   format diagdate yearr4.;


Fluorite | Level 6

sorry for confusing, I repost it to clarify.



Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Get the $99 certification deal.jpg



Back in the Classroom!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 3 in conversation