Help using Base SAS procedures

error about macro

Accepted Solution Solved
Reply
Contributor
Posts: 38
Accepted Solution

error about macro

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)Smiley SadColumn).

      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


Accepted Solutions
Solution
‎06-28-2012 03:33 AM
New Contributor
Posts: 2

Re: error about macro

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


All Replies
Solution
‎06-28-2012 03:33 AM
New Contributor
Posts: 2

Re: error about macro

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

Contributor
Posts: 38

Re: error about macro

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.

Frequent Contributor
Posts: 95

Re: error about macro

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;

Super User
Posts: 9,769

Re: error about macro

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

--->

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

🔒 This topic is solved and locked.

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

Discussion stats
  • 4 replies
  • 4133 views
  • 4 likes
  • 4 in conversation