DATA Step, Macro, Functions and more

Basic MACRO error

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 18
Accepted Solution

Basic MACRO error

Newbie here, need help with basic macro. 
 
I used the following macro:
 
%macro codes (a);
 
data ab.codes ;
set ab.codes ;
if
(
nm1='&a' or 
nm2='&a' or 
nm3='&a' or  
nm4='&a' or 
nm5='&a' 
)
then &a =1 ;
if &a = . and internal=0 then &a=0;
run;
%mend;
 
 
Then I used the following command:
 
%codes ('122');
 
 
 
However, the log gave the following message: 
 
NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set ab.codes may be incomplete.  When this step was stopped there were 0 observations and 33 variables.
WARNING: Data set ab.codes was not replaced because this step was stopped.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds
 
NOTE: Line generated by the macro variable "A".
1      122
       ---
       180
NOTE: Line generated by the macro variable "A".
1      122
       ---
       180
 
ERROR 180-322: Statement is not valid or it is used out of proper order.
 
How can I fix this error?
 
Thank you

Accepted Solutions
Solution
4 weeks ago
Trusted Advisor
Posts: 1,583

Re: Basic MACRO error

[ Edited ]
Posted in reply to sasnewbie12

Run next code and compare to yours original:

%macro codes (a);
 
data ab.codes ;
set ab.codes ;
if
(
nm1="&a" or 
nm2="&a" or 
nm3="&a" or  
nm4="&a" or 
nm5="&a" 
)
then var_&a =1 ;
if var_&a = . and internal=0 then var_&a=0;
run;
%mend;
%codes (122);

Pay attention:

  '&a' is the string of ampersand and the character a.

  "&a" means to replave the macro variable with its vale, that is: "122"

View solution in original post


All Replies
Trusted Advisor
Posts: 1,931

Re: Basic MACRO error

Posted in reply to sasnewbie12

Use

 

options mprint;

 

before your macro, run it again and show us the SAS log, if that doesn't give you enough information for you to solve the problem yourself. 

Occasional Contributor
Posts: 18

Re: Basic MACRO error

Posted in reply to PaigeMiller

4059

4060  options mprint;

4061  %codes (122);

MPRINT(codes):   data ab.codes ;

MPRINT(codes):   set ab.codes ;

MPRINT(codes):   if ( nm1='&a' or nm2='&a' or nm3='&a' or nm4='&a' OR nm5='&a' ) then 122 =1 ;

MPRINT(codes):   if 122 = . and internal=0 then 122=0;

MPRINT(codes):   run;

 

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

WARNING: The data set ab.codes may be incomplete.  When this step was stopped there were 0 observations and 33 variables.

WARNING: Data set ab.codes was not replaced because this step was stopped.

NOTE: DATA statement used (Total process time):

      real time           0.00 seconds

      cpu time            0.00 seconds

 

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

1      122

       ---

       180

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

1      122

       ---

       180

 

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

Occasional Contributor
Posts: 18

Re: Basic MACRO error

Posted in reply to sasnewbie12

So its not reading the &a within ' ' . However, it needs to be within quotations because it is a categorical variable observation. How do I reconcile that?

Trusted Advisor
Posts: 1,931

Re: Basic MACRO error

Posted in reply to sasnewbie12

The macro resolves to a statement which is (in part)

 

then 122=1

 

which makes no sense in SAS

 

 

Occasional Contributor
Posts: 18

Re: Basic MACRO error

Posted in reply to PaigeMiller

Please elaborate how I should fix the syntax within parenthesis.

 

I added a _ before the variable name (then _&a=1) and it created the variable. However, the '&a' within parenthesis is still not being read as 122. 

 

Thank you

Trusted Advisor
Posts: 1,931

Re: Basic MACRO error

Posted in reply to sasnewbie12

Well, I don't know how to fix it, because I don't understand what you are trying to do.

 

But that's irrelevant here. What you need to do before you write any macro, is get the code working without macros, for the simple case where the number of interest is 122.

 

Once you have the code working without macros, then we can turn it into a macro if you still want to do that.

 

So show me the code that works without macros for the simple case where the number of interest is 122.

Solution
4 weeks ago
Trusted Advisor
Posts: 1,583

Re: Basic MACRO error

[ Edited ]
Posted in reply to sasnewbie12

Run next code and compare to yours original:

%macro codes (a);
 
data ab.codes ;
set ab.codes ;
if
(
nm1="&a" or 
nm2="&a" or 
nm3="&a" or  
nm4="&a" or 
nm5="&a" 
)
then var_&a =1 ;
if var_&a = . and internal=0 then var_&a=0;
run;
%mend;
%codes (122);

Pay attention:

  '&a' is the string of ampersand and the character a.

  "&a" means to replave the macro variable with its vale, that is: "122"

Trusted Advisor
Posts: 1,931

Re: Basic MACRO error

@Shmuel, var_@a has never been mentioned, and I doubt this is what the OP wants. This is why he needs to write code without macros that works here, and then we will all understand what he is doing, and then it will likely be easy to turn into a working macro. Without that, I doubt we can proceed.

Trusted Advisor
Posts: 1,583

Re: Basic MACRO error

Posted in reply to PaigeMiller

@PaigeMiller, you are right that var_&a was never mentiond but neither _&a was mentioned,

and geting resolved as122=1 or 122=0 are the core of that error, and it should be replaced with a 

valid variable name.

 

Trusted Advisor
Posts: 1,583

Re: Basic MACRO error

@sasnewbie12, to use the macro run it as %codes(122); without the single quotes.

If it was my typo - I'm going to edit and correct it.

Super User
Posts: 5,516

Re: Basic MACRO error

Posted in reply to sasnewbie12

Three items to note, partially mentioned already ...

 

First, replace all your single quotes with double quotes.  Macro variable references within single quotes will not be resolved (as you have seen).

 

Second, when calling the macro, do not use quotes:  %codes (122) but not %codes ('122')

 

Finally, as you say you intended, add the underscore in a few places:

 

... then _&a = 1;

if _&a = . and internal=0 then _&a = 0;

Occasional Contributor
Posts: 18

Re: Basic MACRO error

Posted in reply to Astounding

Great, you guys solved it.

 

The issues were:

1) using " " instead of  ' ' in order to put the macro value inside of quotes.

2) not starting a variable name with numbers

 

 

Shmuel, you put var before the variable name _&a; is it necessary to put var before adressing a variable? Seems it worked fine after I added the _ 

Trusted Advisor
Posts: 1,583

Re: Basic MACRO error

[ Edited ]
Posted in reply to sasnewbie12

You need a valid sas name.

122 is invalid as sas variable name.

You can change the var_&a to any other prefix or any other valid name 

e.g.:  x&a , prefix_&a.suffix ,  any&a.flag ...etc

 

Pay attention - I have added a dot to assign the end of macro variable name. in order to add more characters

to the variable name.

 

If &a = 122 then any&a.flag = any122flag (without the dot)

if &a = lib  then &a.name = libname (with a single dot), BUT &a..name = lib.name (with double dots)

☑ This topic is solved.

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

Discussion stats
  • 13 replies
  • 147 views
  • 9 likes
  • 4 in conversation