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-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 2 replies
  • 6437 views
  • 1 like
  • 3 in conversation