DATA Step, Macro, Functions and more

scan a numeric macro variable

Reply
Contributor
Posts: 73

scan a numeric macro variable

Dear All,

 

I have code like this:

%let coef = -177 1 -27 -34 -26 -27 -31 -25 -54 -28 -28 -25 -36 -36 7 8 11 50 -9 10 27;

data data;

set data;

score= %scan(&coef,1)*col1 + %scan(&coef,2)*col2 + %scan(&coef,3)*col3;

run;

 

I want to calc score= -117*col1 + 1*col2 + (-27)*col3;

but i realize that %scan ignored the negative signs, so what i actually got is score= 117*col1 + 1*col2 + (27)*col3.

 

Can anyone help me resolve this?

 

Thank you!!

 

Best wishes.

Super User
Posts: 17,818

Re: scan a numeric macro variable

A better method, use a temporary array to store the elements and then a loop. 

 

You have not specified a separator for your terms in the SCAN function. Specify the delimiter/separator. When you create the macro variable list you can specify the delimiter of choice. 

 

 

Also, is there a reason for using %scan instead of scan?

 

Super User
Posts: 5,081

Re: scan a numeric macro variable

To elaborate on one point within Reeza's answer, %SCAN lets you specify blanks only as delimiters in this fashion:

 

%scan(&coef,1,%str( ))

 

Occasionally, you will see a version that is wrong, but usually works properly:

 

%scan(&coef,1, ' ')

 

While this does treat blanks as delimiters, it also treats quotes as delimiters.  Usually that doesn't cause any harm, but that probably was not the intention of the programmer.

Ask a Question
Discussion stats
  • 2 replies
  • 188 views
  • 0 likes
  • 3 in conversation