DATA Step, Macro, Functions and more

Creating a Data Set within in Macro

Accepted Solution Solved
Reply
Contributor
Posts: 33
Accepted Solution

Creating a Data Set within in Macro

Hi,

can any one help me how to overcome the error coming when i am running the below code??

I have used %sysfunc also for firstweek_count variable. But no use..

To get satisfy the condition - %else %If &CurrentQTR. GT 1 %Then %Do;  Please add %Let CurrentQTR = 2 ; before executing...

Because, if the above condition is True, at that time it is giving errros...

Options symbolgen mprint mlogic;

%Global Today Today_mmddyyn8  mmdd Currentweek Currentmonth  CurrentQTR Currentyear Currentqtryear firstweek_count firstCalDayOfQTR_Date9 FirstCalDayofLastYear LastCalDayofLastQTR weekly_dsvars;

Data dates;

    Today = Today();

    Today_mmddyyn8 = Put(Today, mmddyyn8.);

/*    Today_mmddyy10 = Put(Today, mmddyy10.);    */

    Current_mmdd = Substr(Put(Today, mmddyyn8.),1,4);

    Currentweek=Strip(week(Today));

    Currentmonth=Month(Today);

    CurrentQTR = QTR(Today);

    Currentyear = Year(Today);

    Currentqtryear = Put(Today, YYMMN.);

    Call symput("Today", Today);

    Call symput("Today_mmddyyn8", Today_mmddyyn8);

    Call symput("mmdd", Current_mmdd);

    Call symput("Currentweek", Currentweek);

    Call symput("Currentmonth", Currentmonth);

    Call symput("CurrentQTR", CurrentQTR);

    Call symput("Currentyear", Currentyear);

    Call symput("Currentqtryear", Currentqtryear);

Run;

%Put &Today.  &Today_mmddyyn8. &mmdd. &Currentweek. &Currentmonth. &CurrentQTR. &Currentyear. &Currentqtryear.;

%Macro Create_weekly_varlist;

    %Let i=1;

           

    Data qtr_wkly_varlist;

            %If &CurrentQTR. = 1 %Then %Do;

                %Let weekly_dsvars = qtr1_wkly_varlist;

                %Do %while ( &i. LE &Currentweek.);

                    If &i. ne &Currentweek. Then Do;

                        var3 = "week&i._itemfile";

                        var4 = "week&i._vsi";

                        Output;

                    end;

                    else Do;

                        new_var1 = "week&i._itemfile";

                        new_var2 = "week&i._vsi";

                        weekly_count1 = Left(Trimn(%eval(&i.-1)));

                        weekly_count2 = Left(Trimn(&i.));

                        Output;

                    end;

                      %Let i = %eval(&i.+1);

                 %end;

             %end;

            %else %If &CurrentQTR. GT 1 %Then %Do;

                   

                    %If &Currentmonth. in (4,7,10) %Then %Do;

                        firstCalDayOfMonth = intnx('month',&Today.,0);

                        firstCalDayOfQTR_Date9 = Put(firstCalDayOfMonth,Date9.);

                        LastCalDayofLastQTR = Put(intnx('month',&Today.,-1,'end'),mmddyy10.);

                        FirstCalDayofLastYear = Put(intnx('month',&Today.,-13),mmddyy10.);

                        firstweek_count = Week(firstCalDayOfMonth);

                        Call symputx("firstweek_count", firstweek_count);

                        Call symputx("firstCalDayOfQTR_Date9", firstCalDayOfQTR_Date9);

                        Call symputx("LastCalDayofLastQTR", LastCalDayofLastQTR);

                        Call symputx("FirstCalDayofLastYear", FirstCalDayofLastYear);

                    %end;

                    %If &Currentmonth. in (5,8,11) %Then %Do;

                        firstCalDayOfPrevMonth = intnx('month',&Today.,-1);

                        firstCalDayOfQTR_Date9 = Put(firstCalDayOfPrevMonth, Date9.);

                        LastCalDayofLastQTR = Put(intnx('month',&Today.,-2,'end'),mmddyy10.);

                        FirstCalDayofLastYear = Put(intnx('month',&Today.,-14),mmddyy10.);

                        firstweek_count = Week(firstCalDayOfPrevMonth);

                        Call symputx("firstweek_count",firstweek_count);

                        Call symputx("firstCalDayOfQTR_Date9",firstCalDayOfQTR_Date9);

                        Call symputx("LastCalDayofLastQTR", LastCalDayofLastQTR);

                        Call symputx("FirstCalDayofLastYear", FirstCalDayofLastYear);

                    %end;

                    %If &Currentmonth. in (6,9,12) %Then %Do;

                        firstCalDayOfPrev2Month = intnx('month',&Today.,-2);

                        firstCalDayOfQTR_Date9 = Put(firstCalDayOfPrev2Month, Date9.);

                        LastCalDayofLastQTR = Put(intnx('month',&Today.,-3,'end'),mmddyy10.);

                        FirstCalDayofLastYear = Put(intnx('month',&Today.,-15),mmddyy10.);

                        firstweek_count = Week(firstCalDayOfPrev2Month);

                        Call symputx("firstweek_count",firstweek_count);

                        Call symputx("firstCalDayOfQTR_Date9",firstCalDayOfQTR_Date9);

                        Call symputx("LastCalDayofLastQTR", LastCalDayofLastQTR);

                        Call symputx("FirstCalDayofLastYear", FirstCalDayofLastYear);

                    %end;

            %Put &firstweek_count. &LastCalDayofLastQTR. &FirstCalDayofLastYear. &firstCalDayOfQTR_Date9.;

       

            %Do %while ( &i. LE &Currentweek.);

                %If &i. GE &firstweek_count. %then %Do;

                    %Let nextweek_count = %eval(&i.- &firstweek_count. + 1);

                    %If &i. ne &Currentweek. %Then %Do;

                        var3 = "week&nextweek_count._itemfile";

                        var4 = "week&nextweek_count._vsi";

                        Output;

                    %end;

                    %else %Do;

                        new_var1 = "week&nextweek_count._itemfile";

                        new_var2 = "week&nextweek_count._vsi";

                        weekly_count1 = Left(Trimn(%eval(&nextweek_count.-1)));

                        weekly_count2 = Left(Trimn(&nextweek_count.));

                        Output;

                    %end;

                %end;

                %Let i = %eval(&i.+1);

            %end;

        %end;

    Run;

%Mend Create_weekly_varlist;

%Create_weekly_varlist;

Many Thanks,

RaviSPR.


Accepted Solutions
Solution
‎02-17-2014 10:37 AM
Super User
Posts: 7,782

Re: Creating a Data Set within in Macro

Always rember that the macro is executed before the data step is compiled AND before it runs. Any macro variables generated during the run of the data step CANNOT be present at compile and macro execution time, so you will get an error when you %put them during macro execution.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers

View solution in original post


All Replies
Super User
Posts: 11,343

Re: Creating a Data Set within in Macro

Describe the error you are getting. Better post error text from the log. If there is not an error in the log, then post the actual output and the expected output.

Contributor
Posts: 33

Re: Creating a Data Set within in Macro

Options symbolgen mprint mlogic;

%Global Today Today_mmddyyn8  mmdd Currentweek Currentmonth  CurrentQTR Currentyear Currentqtryear firstweek_count firstCalDayOfQTR_Date9 FirstCalDayofLastYear LastCalDayofLastQTR weekly_dsvars;

%Macro test/MINOPERATOR;

    %Let i=1;

    Data qtr_dataset;

                    %If %eval(&Currentmonth. in 4 7 10 ) %Then %Do;

                        firstCalDayOfMonth = intnx('month',&Today.,0);

                        firstCalDayOfQTR_Date9 = Put(firstCalDayOfMonth,Date9.);

                        LastCalDayofLastQTR = Put(intnx('month',&Today.,-1,'end'),mmddyy10.);

                        FirstCalDayofLastYear = Put(intnx('month',&Today.,-13),mmddyy10.);

                        firstweek_count = Week(firstCalDayOfMonth);

                        Call symputx("firstweek_count", firstweek_count);

                        Call symputx("firstCalDayOfQTR_Date9", firstCalDayOfQTR_Date9);

                        Call symputx("LastCalDayofLastQTR", LastCalDayofLastQTR);

                        Call symputx("FirstCalDayofLastYear", FirstCalDayofLastYear);

                    %end;

%Put &firstweek_count. &LastCalDayofLastQTR. &FirstCalDayofLastYear. &firstCalDayOfQTR_Date9.;

%Mend;

%test;

The above macro is running fine and the macro variables are being created without any error and the dataset has been created successfully.

%Let CurrentQTR =2;

%Let Currentmonth = 4;

%Macro Create_weekly_varlist;

    %Let i=1;

          

    Data qtr_wkly_varlist;

            %If &CurrentQTR. = 1 %Then %Do;

                %Let weekly_dsvars = qtr1_wkly_varlist;

                %Do %while ( &i. LE &Currentweek.);

                    If &i. ne &Currentweek. Then Do;

                        var3 = "week&i._itemfile";

                        var4 = "week&i._vsi";

                        Output;

                    end;

                    else Do;

                        new_var1 = "week&i._itemfile";

                        new_var2 = "week&i._vsi";

                        weekly_count1 = Left(Trimn(%eval(&i.-1)));

                        weekly_count2 = Left(Trimn(&i.));

                        Output;

                    end;

                      %Let i = %eval(&i.+1);

                 %end;

             %end;

            %else %If &CurrentQTR. GT 1 %Then %Do;

            

                   %If %eval(&Currentmonth. in 4 7 10 ) %Then %Do;

                        firstCalDayOfMonth = intnx('month',&Today.,0);

                        firstCalDayOfQTR_Date9 = Put(firstCalDayOfMonth,Date9.);

                        LastCalDayofLastQTR = Put(intnx('month',&Today.,-1,'end'),mmddyy10.);

                        FirstCalDayofLastYear = Put(intnx('month',&Today.,-13),mmddyy10.);

                        firstweek_count = Week(firstCalDayOfMonth);

                        Call symputx("firstweek_count", firstweek_count);

                        Call symputx("firstCalDayOfQTR_Date9", firstCalDayOfQTR_Date9);

                        Call symputx("LastCalDayofLastQTR", LastCalDayofLastQTR);

                        Call symputx("FirstCalDayofLastYear", FirstCalDayofLastYear);

                    %end;

                   %If %eval(&Currentmonth. in 5 8 11 ) %Then %Do;

                        firstCalDayOfPrevMonth = intnx('month',&Today.,-1);

                        firstCalDayOfQTR_Date9 = Put(firstCalDayOfPrevMonth, Date9.);

                        LastCalDayofLastQTR = Put(intnx('month',&Today.,-2,'end'),mmddyy10.);

                        FirstCalDayofLastYear = Put(intnx('month',&Today.,-14),mmddyy10.);

                        firstweek_count = Week(firstCalDayOfPrevMonth);

                        Call symputx("firstweek_count",firstweek_count);

                        Call symputx("firstCalDayOfQTR_Date9",firstCalDayOfQTR_Date9);

                        Call symputx("LastCalDayofLastQTR", LastCalDayofLastQTR);

                        Call symputx("FirstCalDayofLastYear", FirstCalDayofLastYear);

                    %end;

                   %If %eval(&Currentmonth. in 6 9 12 ) %Then %Do;

                        firstCalDayOfPrev2Month = intnx('month',&Today.,-2);

                        firstCalDayOfQTR_Date9 = Put(firstCalDayOfPrev2Month, Date9.);

                        LastCalDayofLastQTR = Put(intnx('month',&Today.,-3,'end'),mmddyy10.);

                        FirstCalDayofLastYear = Put(intnx('month',&Today.,-15),mmddyy10.);

                        firstweek_count = Week(firstCalDayOfPrev2Month);

                        Call symputx("firstweek_count",firstweek_count);

                        Call symputx("firstCalDayOfQTR_Date9",firstCalDayOfQTR_Date9);

                        Call symputx("LastCalDayofLastQTR", LastCalDayofLastQTR);

                        Call symputx("FirstCalDayofLastYear", FirstCalDayofLastYear);

                    %end;

            %Put &firstweek_count. &LastCalDayofLastQTR. &FirstCalDayofLastYear. &firstCalDayOfQTR_Date9.;

            %Do %while ( &i. LE &Currentweek.);

                %If &i. GE &firstweek_count. %then %Do;

                    %Let nextweek_count = %eval(&i.- &firstweek_count. + 1);

                    %If &i. ne &Currentweek. %Then %Do;

                        var3 = "week&nextweek_count._itemfile";

                        var4 = "week&nextweek_count._vsi";

                        Output;

                    %end;

                    %else %Do;

                        new_var1 = "week&nextweek_count._itemfile";

                        new_var2 = "week&nextweek_count._vsi";

                        weekly_count1 = Left(Trimn(%eval(&nextweek_count.-1)));

                        weekly_count2 = Left(Trimn(&nextweek_count.));

                        Output;

                    %end;

                %end;

                %Let i = %eval(&i.+1);

            %end;

        %end;

    Run;

%Mend Create_weekly_varlist;

%Create_weekly_varlist;

 

Whereas when i was running the above code, the sas application has got hanged (showing "datastep is running" on top left side of application) and so I had to brake the session.

The problem here is the Macro varibales(&firstweek_count. &LastCalDayofLastQTR. &FirstCalDayofLastYear. &firstCalDayOfQTR_Date9.) are not being resolved with in the %IF CONDITION.( %else %If &CurrentQTR. GT 1 %Then %DoSmiley Wink???

     Here, I need to get the MACRO Variables - &firstweek_count. &LastCalDayofLastQTR. &FirstCalDayofLastYear. &firstCalDayOfQTR_Date9  to be resolved with in the %ELSE %IF condition???

Can you please advice me on this???

Thanks,

RAVISPR.

Solution
‎02-17-2014 10:37 AM
Super User
Posts: 7,782

Re: Creating a Data Set within in Macro

Always rember that the macro is executed before the data step is compiled AND before it runs. Any macro variables generated during the run of the data step CANNOT be present at compile and macro execution time, so you will get an error when you %put them during macro execution.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Contributor
Posts: 33

Re: Creating a Data Set within in Macro

Posted in reply to KurtBremser

Thanks. I will recode the macro again as you mentioned....

Super Contributor
Posts: 282

Re: Creating a Data Set within in Macro

Hi,

If you're getting the error:

ERROR: Required operator not found in expression: &Currentmonth. in (4,7,10)

then it might be worth checking the option minoperator:

SAS(R) 9.2 Macro Language: Reference

The link includes examples of how to use the "in" operator in macro language.

Regards,

Amir.

Contributor
Posts: 33

Re: Creating a Data Set within in Macro

Thanks Amir. Now the above error has been overcome.

Super User
Posts: 19,791

Re: Creating a Data Set within in Macro

Also, look into call symputx.

That looks a bit brutal though, it may be better to consider creating a lookup table work with, I would think. 

Super User
Super User
Posts: 7,043

Re: Creating a Data Set within in Macro

Why are you using macro instead of just writing Data step? It is 10,000x easier to debug a data step than a complex macro loop.

What is it that you are actually trying to accomplish?

🔒 This topic is solved and locked.

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

Discussion stats
  • 8 replies
  • 578 views
  • 0 likes
  • 6 in conversation