BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
alisondu77
Obsidian | Level 7

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

 

1 ACCEPTED SOLUTION

Accepted Solutions
Loko
Barite | Level 11

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

8 REPLIES 8
Loko
Barite | Level 11

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?

alisondu77
Obsidian | Level 7

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

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

Loko
Barite | Level 11

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;

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

alisondu77
Obsidian | Level 7

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 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

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

View all other training opportunities.

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