DATA Step, Macro, Functions and more

Create macro dates in loop

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 5
Accepted Solution

Create macro dates in loop

I wrote below part to get the third month from date macro.

 

%let date=2017-01-01; 
%let a_SASdate=%sysfunc(inputn(&date.,yymmdd10.)) ;
%let b=%sysfunc(putn(&a_SASdate.,yymmn6.)) ;
%let et=%sysfunc(intnx(month,%sysfunc(inputn(&date.,yymmdd10.)),2,s),yymmn6.);
%put  &a_SASdate. &b. &et.;

I wrote below code to create macro variable for each date.

 

data new;
do i=1 to 12;
call symput('mon'||put(i,z2.),put(intnx('month',&et.,i),yymmn6.));
a=symget('mon'||put(i,z2.));
output;
end;
run;

Expected output

 

i   a
1   201704
2   201705
3   201706
4   201707
5   201708
6   201709
7   201710
8   201711
9   201712
10  201801
11  201801
12  201803

But what iam getting is

 

1   251204
2   251205
3   251206
4   251207
5   251208
6   251209
7   251210
8   251211
9   251212
10  251301
11  251302
12  251303

What went wrong?


Accepted Solutions
Solution
‎05-07-2018 09:43 AM
Super User
Posts: 9,925

Re: Create macro dates in loop

Posted in reply to geetha1a2b3c

There's a comma right after putn that does not belong there.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code

View solution in original post


All Replies
PROC Star
Posts: 1,218

Re: Create macro dates in loop

Posted in reply to geetha1a2b3c

Not sure where your error is since I have not checked. 

 

However, you are overcomplicating things massively in my opinion. You can do like this to get your desired result

 

data dates(keep= i a);
    date='01jan2017'd;
    monthsahead=3;
    do i=1 to 12;
        a=intnx('month', date, i+months-1, 'sameday');
        output;
    end;
    format a yymmn6.;
run;
Occasional Contributor
Posts: 5

Re: Create macro dates in loop

Thanks draycut. I am looking for what went wrong. Why iam getting different result.I gonna use 'mon'||put(i,z2.) in later part of my code
Super User
Super User
Posts: 7,942

Re: Create macro dates in loop

Posted in reply to geetha1a2b3c

The data step is fine.

%let et="&sysdate9"d ;
data new;
length i 8 a $6 ;
do i=1 to 12;
call symputX('mon'||put(i,z2.),put(intnx('month',&et.,i),yymmn6.));
a=symget('mon'||put(i,z2.));
output;
 put i= a= ;
end;
run;

But it is assuming that ET contains a date value, and it does not.

Why not just store the date value into ET?

%let et=%sysfunc(intnx(month,%sysfunc(inputn(&date.,yymmdd10.)),2,s));

 

Occasional Contributor
Posts: 5

Re: Create macro dates in loop

I am trying use the above resolved like


%let perf=12;
%macro addall(dname,rrun);
%do j=1 %to &rrun;
%let z=%sysfunc(putn,(&j,z2.));
&dname._&&mon.&z;
%end;
%mend addall;


data new1;
set %addall(new,&perf.);
run;


Getting below error
data new1;
64
65 set %addall(new,&perf.);
ERROR: Expected open parenthesis after macro function name not found.
NOTE: Line generated by the macro variable "Z".
65 mon.(1,z2.))
_ _
22 22
200
ERROR 22-7: Invalid option name 1.

ERROR 22-322: Syntax error, expecting one of the following: a name, a quoted string, ;, CUROBS, END, INDSNAME, KEY, KEYRESET, KEYS,
NOBS, OPEN, POINT, _DATA_, _LAST_, _NULL_.

ERROR 200-322: The symbol is not recognized and will be ignored.

NOTE: Line generated by the macro variable "Z".
65 new_&mon.
_
22
200
WARNING: Apparent symbolic reference MON not resolved.
ERROR: Expected open parenthesis after macro function name not found.
NOTE: Line generated by the macro variable "Z".
65 new_&mon.
____
180
WARNING: Apparent symbolic reference MON not resolved.
ERROR: Expected open parenthesis after macro function name not found.
NOTE: Line generated by the macro variable "Z".
65 new_&mon.
____
180
WARNING: Apparent symbolic reference MON not resolved.
ERROR: Expected open parenthesis after macro function name not found.
NOTE: Line generated by the macro variable "Z".
65 new_&mon.
____
180
WARNING: Apparent symbolic reference MON not resolved.
ERROR: Expected open parenthesis after macro function name not found.
NOTE: Line generated by the macro variable "Z".
65 new_&mon.
____
180
WARNING: Apparent symbolic reference MON not resolved.
ERROR: Expected open parenthesis after macro function name not found.
NOTE: Line generated by the macro variable "Z".
65 new_&mon.
____
180
WARNING: Apparent symbolic reference MON not resolved.
ERROR: Expected open parenthesis after macro function name not found.
NOTE: Line generated by the macro variable "Z".
65 new_&mon.
____
180
WARNING: Apparent symbolic reference MON not resolved.
ERROR: Expected open parenthesis after macro function name not found.
NOTE: Line generated by the macro variable "Z".
65 new_&mon.
____
180

Solution
‎05-07-2018 09:43 AM
Super User
Posts: 9,925

Re: Create macro dates in loop

Posted in reply to geetha1a2b3c

There's a comma right after putn that does not belong there.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
☑ This topic is solved.

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

Discussion stats
  • 5 replies
  • 121 views
  • 1 like
  • 4 in conversation