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

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;

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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=01OCT2011

given_month=10

given_day=01

given_year=2011

next_Month=01NOV2011

this_Qtr=01OCT2011

last_Qtr=01JUL2011

last_yr=01OCT2010

prev_yr1=01OCT2010

prev_yr2=01OCT2009

prev_yr3=01OCT2008

prev_yr4=01OCT2007

prev_yr5=01OCT2006

prev_yr6=01OCT2005

prev_yr7=01OCT2004

i=8

View solution in original post

5 REPLIES 5
art297
Opal | Level 21

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.

BobyGadu
Calcite | Level 5

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;

Tom
Super User Tom
Super User

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?

Tom
Super User Tom
Super User

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=01OCT2011

given_month=10

given_day=01

given_year=2011

next_Month=01NOV2011

this_Qtr=01OCT2011

last_Qtr=01JUL2011

last_yr=01OCT2010

prev_yr1=01OCT2010

prev_yr2=01OCT2009

prev_yr3=01OCT2008

prev_yr4=01OCT2007

prev_yr5=01OCT2006

prev_yr6=01OCT2005

prev_yr7=01OCT2004

i=8

BobyGadu
Calcite | Level 5

Thanks a lot :smileycool:

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 2266 views
  • 0 likes
  • 3 in conversation