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

Hi all,

I made some codes including macro. They went well when I used last time, but I always found errors when using them today. Pretty weird!

I have listed the code and log message below. Could anybody help me to find out the reason? Thanks.

SAS code:

libname expe "D:\...\Desktop\experiment\Ian";

%let f1     = 61;       /* abu dhabi */

%let f2     = 180;      /* argentina */

%let f3     = 200;      /* australia */

%let f4     = 100;      /* australia */

%let f5     = 100;      /* australia */

%let f6     = 200;      /* australia */

%let c1     = %eval(31*&f1);

%macro initialize;

%do j= 2 %to 5;

      %let k = %eval(&j-1);

      %let c&j = %eval(&&c&k + 31*&&f&j

%end;

%mend;

%macro step1;

data expe_step1;

      set  expe.expe;

      if _N_ <= &c1 then countryid=1;

      %do i = 2 %to 5;

            else if _N_ <= &&c&i then countryid = &i;

      %end;

        else countryid=6;

run;

%mend step1;

/*macro step1 is equivalent to the following procedures:

data expe_step1;

      set  expe;

      if _N_ <= 1891 then countryid=1;

        else if _N_ <= 1891+5580 then countryid=2;

        else if _N_ <= 1891+5580+6200 then countryid=3;

        else if _N_ <= 1891+5580+6200+3100 then countryid=4;

        else if _N_ <= 1891+5580+6200+3100+3100 then countryid=5;

        else countryid=6;

run;*/

%initialize

%step1

Log file:

33   %initialize

34   %step1

NOTE: Line generated by the macro variable "I".

1     &c2

      -

      22

WARNING: Apparent symbolic reference C2 not resolved.

NOTE: Line generated by the invoked macro "STEP1".

3             else if _N_ <= &&c&i then countryid = &i;

                                        ---------

                                        180

ERROR 22-322: Syntax error, expecting one of the following: a name, a quoted string,

              a numeric constant, a datetime constant, a missing value, bitstring, INPUT, PUT.

ERROR 180-322: Statement is not valid or it is used out of proper order.

NOTE: Line generated by the invoked macro "STEP1".

3             else if _N_ <= &&c&i then countryid = &i;

                                                  -

                                                  180

ERROR 180-322: Statement is not valid or it is used out of proper order.

NOTE: Line generated by the macro variable "I".

1     &c3

      -

      22

WARNING: Apparent symbolic reference C3 not resolved.

NOTE: Line generated by the invoked macro "STEP1".

5             else if _N_ <= &&c&i then countryid = &i;

                                        ---------

                                        180

ERROR 22-322: Syntax error, expecting one of the following: a name, a quoted string,

              a numeric constant, a datetime constant, a missing value, bitstring, INPUT, PUT.

ERROR 180-322: Statement is not valid or it is used out of proper order.

NOTE: Line generated by the invoked macro "STEP1".

5             else if _N_ <= &&c&i then countryid = &i;

                                                  -

                                                  180

ERROR 180-322: Statement is not valid or it is used out of proper order.

NOTE: Line generated by the macro variable "I".

1     &c4

      -

      22

WARNING: Apparent symbolic reference C4 not resolved.

NOTE: Line generated by the invoked macro "STEP1".

7             else if _N_ <= &&c&i then countryid = &i;

                                        ---------

                                        180

ERROR 22-322: Syntax error, expecting one of the following: a name, a quoted string,

              a numeric constant, a datetime constant, a missing value, bitstring, INPUT, PUT.

ERROR 180-322: Statement is not valid or it is used out of proper order.

NOTE: Line generated by the invoked macro "STEP1".

7             else if _N_ <= &&c&i then countryid = &i;

                                                  -

                                                  180

ERROR 180-322: Statement is not valid or it is used out of proper order.

NOTE: Line generated by the macro variable "I".

1     &c5

      -

      22

WARNING: Apparent symbolic reference C5 not resolved.

NOTE: Line generated by the invoked macro "STEP1".

9             else if _N_ <= &&c&i then countryid = &i;

                                        ---------

                                        180

ERROR 22-322: Syntax error, expecting one of the following: a name, a quoted string,

              a numeric constant, a datetime constant, a missing value, bitstring, INPUT, PUT.

ERROR 180-322: Statement is not valid or it is used out of proper order.

NOTE: Line generated by the invoked macro "STEP1".

9             else if _N_ <= &&c&i then countryid = &i;

                                                  -

                                                  180

ERROR 180-322: Statement is not valid or it is used out of proper order.

NOTE: Character values have been converted to numeric values at the places given by:

      (Line):(Column).

      0:-1                                     <no line/column information available>

NOTE: The SAS System stopped processing this step because of errors.

WARNING: The data set WORK.EXPE_STEP1 may be incomplete.  When this step was stopped there were

         0 observations and 9 variables.

WARNING: Data set WORK.EXPE_STEP1 was not replaced because this step was stopped.

NOTE: DATA statement used (Total process time):

      real time           0.04 seconds

      cpu time            0.01 seconds

1 ACCEPTED SOLUTION

Accepted Solutions
Cloggy
Calcite | Level 5

Hi Comeon2012,

Some things that I notice:

In macro Initialize, the following line needs a closing bracket and a semicolon to end the line:

      %let c&j = %eval(&&c&k + 31*&&f&j

=>

      %let c&j = %eval(&&c&k + 31*&&f&j);


and the macro variables C2 till C5 have to be defined global. As they are defined now within the macro initialize, they will only be known inside that macro as being local macro variables. a line like:

      %global c2 c3 c4 c5;

or inside the macro initialize:

%macro initialize;

%do j= 2 %to 5;

      %let k = %eval(&j-1);

    %global c&j;

      %let c&j = %eval(&&c&k + 31*&&f&j

%end;

%mend;

should take care of that.

Hope this helps!

Cloggy

View solution in original post

4 REPLIES 4
Cloggy
Calcite | Level 5

Hi Comeon2012,

Some things that I notice:

In macro Initialize, the following line needs a closing bracket and a semicolon to end the line:

      %let c&j = %eval(&&c&k + 31*&&f&j

=>

      %let c&j = %eval(&&c&k + 31*&&f&j);


and the macro variables C2 till C5 have to be defined global. As they are defined now within the macro initialize, they will only be known inside that macro as being local macro variables. a line like:

      %global c2 c3 c4 c5;

or inside the macro initialize:

%macro initialize;

%do j= 2 %to 5;

      %let k = %eval(&j-1);

    %global c&j;

      %let c&j = %eval(&&c&k + 31*&&f&j

%end;

%mend;

should take care of that.

Hope this helps!

Cloggy

comeon2012
Fluorite | Level 6

Hi Cloggy & Alpay,

Thanks for your suggestion. It is really of great help.

The problem is just due to that the the macro variable c&j is incorrectly taken as local variable.

For the clsoing bracket, I included it in my code but missed it when I posted the thread.

Thanks again.

Alpay
Fluorite | Level 6

You are referring to c2 to c6 macro variables defined inside intialize macro. Their scope is the issue here (local macro variable, not known outside of the macro in which they are defined). Also, I did not see a closing parenthesis and semicolon at the end of %let statement. Added "%global c&j;" statement. Added ");". Can you try with the following macro defintion? %macro initialize;   %do j= 2 %to 5;          %let k = %eval(&j-1);          %global c&j;          %let c&j = %eval(&&c&k + 31*&&f&j);   %end; %mend;

Ksharp
Super User

%let c&j = %eval(&&c&k + 31*&&f&j

--->

%let c&j = %eval(&&c&k + 31*&&f&j ) ;                ?

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 4 replies
  • 9234 views
  • 4 likes
  • 4 in conversation