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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

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.

View solution in original post

8 REPLIES 8
ballardw
Super User

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.

RaviSPR
Obsidian | Level 7

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 %Do;)???

     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.

Kurt_Bremser
Super User

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.

RaviSPR
Obsidian | Level 7

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

Amir
PROC Star

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.

RaviSPR
Obsidian | Level 7

Thanks Amir. Now the above error has been overcome.

Reeza
Super User

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. 

Tom
Super User Tom
Super User

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?

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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