SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ilikesas
Barite | Level 11

Hi,

 

I have a list of macro variables var1, var2 ... var&cnt that corresponds to variables.

 

For example, &var1 = price. I have a dataset with variables price_qc and price_cc, and if price_qc ne . then I want the new variable price = price_qc. Else, that is if price_qc is . , I want price = price_cc.

 

I am trying to do the following code:

 

%macro vars;
data want;
set have;
%do i = 1 %to &cnt;
%if &&var&i&'_qc' ne .   %then 
&&var&i = &&var&i&'_qc'  ;
%else 
&&var,i = &&var&i&'_cc';

%end;
run;
%mend;

%vars;

But I keep getting an error message:

 

"A character function was found in the %EVAL function or %IF condition where a numeric operand is required. The condition was:

&&var&i&'_qc' ne . "

 

I tried different possibilities of operands, but the realized that even the = operand gets the same message.

 

Thank you

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

A few changes:  removing quotes, removing the extra &, adding . to delimit macro variable names, changing %IF to IF:

 

%do i=1 %to &cnt;

 

if &&var&i.._qc ne . then &&var&i = &&var&i.._qc;

else &&var&i = &&var&i.._cc;

 

%end;

 

Also note, you may find it handy to examine the COALESCE function.

View solution in original post

2 REPLIES 2
error_prone
Barite | Level 11
Get rid of the qoutes around _qc and _cc. Hardly necessary to quote anything in macro code.
Astounding
PROC Star

A few changes:  removing quotes, removing the extra &, adding . to delimit macro variable names, changing %IF to IF:

 

%do i=1 %to &cnt;

 

if &&var&i.._qc ne . then &&var&i = &&var&i.._qc;

else &&var&i = &&var&i.._cc;

 

%end;

 

Also note, you may find it handy to examine the COALESCE function.

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 7534 views
  • 1 like
  • 3 in conversation