Solved
Contributor
Posts: 30

Dynamic Array assigning dates

I need to put dates into the macro variable has to generare dynamically but the all the code execution is good but the main problem is in symput the generation of macro variable and assigning the date value of array to the macrovariable generared please help me out from this

&P_B_mm is 01OCT2011

%macro lp;

data dats5 ;

given_date           =           "'&P_B_mm'"d ;

given_month          =           month(given_date) ;

given_day           =           day(given_date) ;

given_year          =           year(given_date) ;

next_Month          =          intnx('month',"'&P_B_mm'"d,1, 'b');

last_Qtr          =          intnx('month',"'&P_B_mm'"d,-2, 'b');

last_yr                    =          intnx('year',"'&P_B_mm'"d,-1, 's');

*Array for storing dates ;

array prev_yr[7];

%let i=7 ;

%do i=1 %to &i;

prev_yr(&i)=          intnx('year',"'&P_B_mm'"d,(-&i), 's');

temp=prev_yr(&i);

put temp= ;

Call Symput( 'DD&i' , put( input( "temp" , d ) , d )) ;

%put &&DY&i;

format prev_yr1-prev_yr7 date9.;

%end;

format given_date Next_month last_Qtr last_yr date9. ;

run;

%mend lp;%lp;run;

Accepted Solutions
Solution
‎11-29-2011 11:49 PM
Super User
Posts: 8,127

Re: Dynamic Array assigning dates

Is this what you want to do?

Why are you adding three leading spaces inside the quotes of the macro variable P_YEAR?

I added a THIS_QTR variable so that you can check if the LAST_QTR variable is getting set the way you want.

`%let Date =   31OCT2011 ;%let Year_mm  =  201110;%let B_mm =   01OCT2011;data _null_ ;  call symput( 'P_Date' , "'"||put( "&Date"d , yymmddd10. )||"'" ) ;  call symput( 'P_Year' , "'   &year_mm'");  call symput( 'P_B_mm' , "'&B_mm'" ) ;run ;%put p_date=&p_date p_year=&p_year p_b_mm=&p_b_mm;data dats5;  given_date = "&B_mm"d ;  given_month= month(given_date) ;  given_day  = day(given_date) ;  given_year = year(given_date) ;  next_Month = intnx('month',given_date,1,'b');  this_Qtr   = intnx('qtr',given_date,0, 'b');  last_Qtr   = intnx('qtr',given_date,-1, 'b');  last_yr    = intnx('year',given_date,-1,'s');  array prev_yr[7];  do i=1 to dim(prev_yr);    prev_yr(i)=  intnx('year',given_date,-i,'s');    call symputx( cats('DY',i) , put(prev_yr(i),date9.)) ;  end;  format _numeric_ date9. given_month given_day z2. given_year i ;  put (_all_) (=/);run;`

Here are the output of the %PUT and PUT statements.

`p_date='2011-10-31' p_year='   201110' p_b_mm='01OCT2011'given_date=01OCT2011given_month=10given_day=01given_year=2011next_Month=01NOV2011this_Qtr=01OCT2011last_Qtr=01JUL2011last_yr=01OCT2010prev_yr1=01OCT2010prev_yr2=01OCT2009prev_yr3=01OCT2008prev_yr4=01OCT2007prev_yr5=01OCT2006prev_yr6=01OCT2005prev_yr7=01OCT2004i=8`

All Replies
PROC Star
Posts: 8,167

Dynamic Array assigning dates

You need to provide more info/code.  Why aren't you simply passing &P_B_mm into the macro, rather than assigning it outside of the macro.  At one point in your macro you refer to a macro variable, but have it in single rather than double quotes.  It won't resolve with just single quotes.  Then, in the macro, you refer to something called d, but have never defined what that is.

Contributor
Posts: 30

Dynamic Array assigning dates

this is the full code

%let Date       =           31OCT2011 ;

%let Year_mm    =          201110;

%let B_mm       =           01OCT2011;

Data _null_ ;

Call Symput( 'P_Date' , "'"||put( input( "&Date" , Date9. ) , yymmddd10. )||"'" ) ;

Call Symput( 'P_Year' , "'"||put( input( "&Year_mm" , yymmn6. ) , yymmn9. )||"'" ) ;

Call Symput( 'P_B_mm' , "'"||put( input( "&B_mm" , date9. ) , date9. )||"'" ) ;

run ;

%macro lp;

data dats5 ;

given_date           =           "'&P_B_mm'"d ;

given_month          =           month(given_date) ;

given_day           =           day(given_date) ;

given_year          =           year(given_date) ;

next_Month          =          intnx('month',"'&P_B_mm'"d,1, 'b');

last_Qtr          =          intnx('month',"'&P_B_mm'"d,-2, 'b');

last_yr                    =          intnx('year',"'&P_B_mm'"d,-1, 's');

*Array for storing dates ;

array prev_yr[7];

array pv[7];

%let i=7 ;

%do i=1 %to &i;

%put i=&i;

prev_yr(&i)=          intnx('year',"'&P_B_mm'"d,(-&i), 's');

temp=prev_yr(&i);

put temp= ;

/*                                                  Call symput("DY&i",'temp');*/

Call Symput( "DY&i" , put(  "temp" , date9. )date9. ) ;

%put &&DY&i;

format prev_yr1-prev_yr7 date9.;

%end;

/*                    put _all_ ;*/

format given_date Next_month last_Qtr last_yr date9. ;

run;

%mend lp;%lp;run;

Super User
Posts: 8,127

Re: Dynamic Array assigning dates

What are you trying to do? Can you explain it in words?

Why are you using a macro loop to loop over a data step array?  Why not just use an normal DO loop?  In fact why do you have a macro at all?

Why are you creating so many different macro variables in different date formats?  You only seem to be using &P_B_MM in the program.

What are you planning to do with the dataset DATS5 that you are generating?

Solution
‎11-29-2011 11:49 PM
Super User
Posts: 8,127

Re: Dynamic Array assigning dates

Is this what you want to do?

Why are you adding three leading spaces inside the quotes of the macro variable P_YEAR?

I added a THIS_QTR variable so that you can check if the LAST_QTR variable is getting set the way you want.

`%let Date =   31OCT2011 ;%let Year_mm  =  201110;%let B_mm =   01OCT2011;data _null_ ;  call symput( 'P_Date' , "'"||put( "&Date"d , yymmddd10. )||"'" ) ;  call symput( 'P_Year' , "'   &year_mm'");  call symput( 'P_B_mm' , "'&B_mm'" ) ;run ;%put p_date=&p_date p_year=&p_year p_b_mm=&p_b_mm;data dats5;  given_date = "&B_mm"d ;  given_month= month(given_date) ;  given_day  = day(given_date) ;  given_year = year(given_date) ;  next_Month = intnx('month',given_date,1,'b');  this_Qtr   = intnx('qtr',given_date,0, 'b');  last_Qtr   = intnx('qtr',given_date,-1, 'b');  last_yr    = intnx('year',given_date,-1,'s');  array prev_yr[7];  do i=1 to dim(prev_yr);    prev_yr(i)=  intnx('year',given_date,-i,'s');    call symputx( cats('DY',i) , put(prev_yr(i),date9.)) ;  end;  format _numeric_ date9. given_month given_day z2. given_year i ;  put (_all_) (=/);run;`

Here are the output of the %PUT and PUT statements.

`p_date='2011-10-31' p_year='   201110' p_b_mm='01OCT2011'given_date=01OCT2011given_month=10given_day=01given_year=2011next_Month=01NOV2011this_Qtr=01OCT2011last_Qtr=01JUL2011last_yr=01OCT2010prev_yr1=01OCT2010prev_yr2=01OCT2009prev_yr3=01OCT2008prev_yr4=01OCT2007prev_yr5=01OCT2006prev_yr6=01OCT2005prev_yr7=01OCT2004i=8`
Contributor
Posts: 30

Dynamic Array assigning dates

Thanks a lot :smileycool:

🔒 This topic is solved and locked.