BookmarkSubscribeRSS Feed
Xiaoningdemao
Quartz | Level 8

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.

2 REPLIES 2
Reeza
Super User

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?

 

Astounding
PROC Star

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.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 2284 views
  • 0 likes
  • 3 in conversation