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"
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.
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.
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?
The macro resolves to a statement which is (in part)
then 122=1
which makes no sense in SAS
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
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.
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"
@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.
@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.
@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.
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;
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 _
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)
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.