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.

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