Help using Base SAS procedures

Comparing a date variable to a year, to calculate how many date variables are in this year

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 15
Accepted Solution

Comparing a date variable to a year, to calculate how many date variables are in this year

Hello everyone,

I have a little problem in my script SAS.

I have a starting date (01jan2015), and I add to this date one month eleven times  (01jan2015, 01feb2015, ..., 01dec2015).
There is no problem for that, but after I would like to count how many dates are in 2015 or 2014, and i don't succeed... I just have "1" in output for nb_month_2014 and nb_month_2015, which is wrong. I should have : nb_month_2014= 0 and nb_month_2015=12.


 

%let date_start = "01jan2015"d; 
%let nb_month_2014=0;
%let nb_month_2015=0;
data _null_;
 do i=0 to 11 ;
 	dt=intnx("month",&date_start.,i,'same');
 	if (put(year(dt),z4.)=2014) then %let nb_month_2014 = %eval(&nb_month_2014 + 1) ;
 	if (put(year(dt),z4.)=2015) then %let nb_month_2015 = %eval(&nb_month_2015 + 1) ;
 end;
run;
%put nb_month_2014 = &nb_month_2014. ;
%put nb_month_2015 = &nb_month_2015. ;


Thank you for helping me ! and sorry for my poor english !

Alison

 


Accepted Solutions
Solution
‎04-25-2016 06:58 AM
Super Contributor
Posts: 308

Re: Comparing a date variable to a year, to calculate how many date variables are in this year

Posted in reply to alisondu77

hello,

 

%macro test;
%let date_start = %sysfunc(inputn(01jan2015,date9.));
%let nb_month_2014=0;
%let nb_month_2015=0;
%do i=0 %to 11;

%let dt=%sysfunc(intnx(month,&date_start.,&i,same));
%if %sysfunc(year(&dt)) eq 2014 %then %let nb_month_2014 = %eval(&nb_month_2014 + 1) ;
%if %sysfunc(year(&dt)) eq 2015 %then %let nb_month_2015 = %eval(&nb_month_2015 + 1) ;

%end;

%put nb_month_2014 = &nb_month_2014. ;
%put nb_month_2015 = &nb_month_2015. ;


%mend test;

%test;

View solution in original post


All Replies
Super Contributor
Posts: 308

Re: Comparing a date variable to a year, to calculate how many date variables are in this year

Posted in reply to alisondu77

Hello,

 

This happenns beacuse you mix the macro statements within a data step. If you use macro statements/variables why you don't put them in a macro?

Occasional Contributor
Posts: 15

Re: Comparing a date variable to a year, to calculate how many date variables are in this year

Thank you ! So, I should do something like this :

%macro test() ;
%let date_start = "01jan2015"d; 
%let nb_month_2014=0;
%let nb_month_2015=0;
%do i=0 %to 11 ;
 	%let dt=%sysfunc(intnx("month",&date_start.,&i.,'same'));
 	%if put(%sysfunc(year(&dt.),z4.))=2014 %then %let nb_month_2014 = %eval(&nb_month_2014 + 1) ;
 	%if put(%sysfunc(year(&dt.),z4.))=2015  %then %let nb_month_2015 = %eval(&nb_month_2015 + 1) ;
 %end;
%mend ;
%test() 
%put nb_month_2014 = &nb_month_2014. ;
%put nb_month_2015 = &nb_month_2015. ;

I don't manage my errors, I am a beginner in macro ... Can you help me to find what's wrong in my script ? Thank you in advance !


Alison

Super User
Super User
Posts: 7,942

Re: Comparing a date variable to a year, to calculate how many date variables are in this year

Posted in reply to alisondu77

As noted in my post below, this is a very good example of trying to force a very simple Base SAS task through the macro pre-processor which isn't built for writing code.

Solution
‎04-25-2016 06:58 AM
Super Contributor
Posts: 308

Re: Comparing a date variable to a year, to calculate how many date variables are in this year

Posted in reply to alisondu77

hello,

 

%macro test;
%let date_start = %sysfunc(inputn(01jan2015,date9.));
%let nb_month_2014=0;
%let nb_month_2015=0;
%do i=0 %to 11;

%let dt=%sysfunc(intnx(month,&date_start.,&i,same));
%if %sysfunc(year(&dt)) eq 2014 %then %let nb_month_2014 = %eval(&nb_month_2014 + 1) ;
%if %sysfunc(year(&dt)) eq 2015 %then %let nb_month_2015 = %eval(&nb_month_2015 + 1) ;

%end;

%put nb_month_2014 = &nb_month_2014. ;
%put nb_month_2015 = &nb_month_2015. ;


%mend test;

%test;

Super User
Super User
Posts: 7,942

Re: Comparing a date variable to a year, to calculate how many date variables are in this year

Posted in reply to alisondu77

What is it your trying to do.  Post test data in the form of a datastep and what you want the output to look like.  The reason I ask is because most of this code is not neccessary.  If your couting interval's then there are functions for this, I can provide code, but I need to see the data and what the output should look like.  In your code you are mixing Macro language and Base SAS code which doesn't work.  Macro is text generator, and happens before any code actually runs.  Hence the if statements are evaulated out before the code is even passed to the compiler.  In almost all instances you will not need to use Macro language, it does have its place, however most tasks can be done directly in Base SAS.

Super User
Posts: 19,770

Re: Comparing a date variable to a year, to calculate how many date variables are in this year

Posted in reply to alisondu77

Why are you using macro variables at all? 

Occasional Contributor
Posts: 15

Re: Comparing a date variable to a year, to calculate how many date variables are in this year

You have surely right, I didn't manage very well "counting" procedures that's why, unfornunately, I make complicate things for "simple" things. Sorry I am still student and didn't practice so much on data procedures... :/ I'm trying to learn by myself :/

Thank you all for your advice and taking time for me !

Alison 

Super User
Super User
Posts: 7,942

Re: Comparing a date variable to a year, to calculate how many date variables are in this year

Posted in reply to alisondu77

Here is a datastep version, it is deliberately verbose to show the working of the date increment functions.  You could even do the same thing for many years, but if you were to do that then either an array, or creating a big output dataset and then aggregate function on it:

%let date_start="01JAN2015"d;
data _null_;
  d=&date_start.;
  do i=0 to 11;
    year2014=sum(year2014,ifn(year(d)=2014,1,0));
    year2015=sum(year2015,ifn(year(d)=2015,1,0));
    d=intnx('month',d,1);
  end;
  put "2014=" year2014 ", 2015=" year2015;
run;
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 8 replies
  • 383 views
  • 2 likes
  • 4 in conversation