Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- Home
- /
- Programming
- /
- SAS Procedures
- /
- Comparing a date variable to a year, to calculate how many date variab...

Options

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

🔒 This topic is **solved** and **locked**.
Need further help from the community? Please
sign in and ask a **new** question.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Posted 04-25-2016 05:31 AM
(1316 views)

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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;

8 REPLIES 8

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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?

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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;

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Why are you using macro variables at all?

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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

Alison

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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;

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. **Registration is now open through August 30th**. Visit the SAS Hackathon homepage.

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.