hi, i am struggle to give a name to a variable which will include the name of my initial variables
My initial variables are var1 and var2 i want the below code to create a new using the existing names of var1/var2
For example, i want a variable which will be named as var1_cl
I don't understand while the &&var&i.. resolved in my if statement, it is not resolved when it comes to names
the issue is in the &&var&i.._cl ="ok" statement where i receive the below error
ERROR 180-322: Statement is not valid or it is used out of proper order.
data test;
input var1 var2;
datalines;
3 5
run;
%macro example(vars=);
%let num_vars = %sysfunc(countw(%superq(vars)));
%do i = 1 %to &num_vars.;
%let var&i. = %qscan(%superq(vars), &i., %str( ));
%put var&i. = &&var&i..;
%end;
%do i = 1 %to &num_vars.;
data test_&i;
set test;
if &&var&i.. > 1 then &&var&i.._cl ="ok";
run;
%end;
%mend;
%example(vars=var1 var2);
Note sure why you are using %qscan (much less %superq)
This doesn't throw the errors:
%macro example(vars=); %let num_vars = %sysfunc(countw(%superq(vars))); %do i = 1 %to &num_vars.; %let var&i. = %scan(%superq(vars), &i.); %put var&i. = &&var&i.; %end; %do i = 1 %to &num_vars.; data test_&i; set test; if &&var&i.. > 1 then &&var&i.._cl ="ok"; run; %end; %mend; %example(vars=var1 var2);
So I suspect that the Qscan is partially the issue in how it protects the expected "special or mnemonic characters", i.e. the & or %
If you are expecting to use variable names with the special characters, or indeed anything other that character, digit and underscore you are referencing the variables incorrectly everywhere as they would have to be in the name literal form of "some%name*char"n , quotes plus the n.
Do not add macro quoting to the name pulled from the list. That will confuse the parser when into thinking there are two names instead of one. Anything that can be a valid SAS variable name will not require macro quoting.
Do you really want to make N separate datasets? Or did you want to make N separate variables?
If the latter then your %DO loop is in the wrong place.
There is no need to deal with macro variable names with numeric suffixes for this problem. Just re-use the same macro variable to hold the next name in the list.
%macro example(vars=,prefix=,suffix=);
%local i num_vars var;
%let num_vars = %sysfunc(countw(%superq(vars),%str( )));
data want;
set have;
%do i = 1 %to &num_vars.;
%let var = %scan(%superq(vars),&i,%str( ));
if &var > 1 then &prefix.&var.&suffix ="ok";
%end;
run;
%mend;
data have;
set sashelp.class;
run;
options mprint;
%example(vars=age height weight,suffix=_cl);
378 options mprint; 379 %example(vars=age height weight,suffix=_cl); MPRINT(EXAMPLE): data want; MPRINT(EXAMPLE): set have; MPRINT(EXAMPLE): if age > 1 then age_cl ="ok"; MPRINT(EXAMPLE): if height > 1 then height_cl ="ok"; MPRINT(EXAMPLE): if weight > 1 then weight_cl ="ok"; MPRINT(EXAMPLE): run; NOTE: There were 19 observations read from the data set WORK.HAVE. NOTE: The data set WORK.WANT has 19 observations and 8 variables.
Note sure why you are using %qscan (much less %superq)
This doesn't throw the errors:
%macro example(vars=); %let num_vars = %sysfunc(countw(%superq(vars))); %do i = 1 %to &num_vars.; %let var&i. = %scan(%superq(vars), &i.); %put var&i. = &&var&i.; %end; %do i = 1 %to &num_vars.; data test_&i; set test; if &&var&i.. > 1 then &&var&i.._cl ="ok"; run; %end; %mend; %example(vars=var1 var2);
So I suspect that the Qscan is partially the issue in how it protects the expected "special or mnemonic characters", i.e. the & or %
If you are expecting to use variable names with the special characters, or indeed anything other that character, digit and underscore you are referencing the variables incorrectly everywhere as they would have to be in the name literal form of "some%name*char"n , quotes plus the n.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.