BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Toni2
Lapis Lazuli | Level 10

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);
1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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.

View solution in original post

3 REPLIES 3
Tom
Super User Tom
Super User

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.

 

Toni2
Lapis Lazuli | Level 10
great, thanks
ballardw
Super User

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.

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!

How to Concatenate Values

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.

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
  • 3 replies
  • 408 views
  • 2 likes
  • 3 in conversation