BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ffgsdf
Obsidian | Level 7

hi every one,

 

I am try to write a k-means algorithm in one dimension data in sas macro. 

 

 

 

First step, I trying to randomly sample three object from 1 to 13 and name them as k1, k2, and k3.

 

second step, I want to calculate each the difference between my variable and k1 to t3 to decide which one belongs to which cluster.

 

However, i cannot calcuate the macro variable in sas. 

 

the following is my code. 

 

data t;

INPUT x;
cards;
1
2
3
4
5
6
7
8
9
10
11
12
13
;
%macro a(data,k,var);
proc surveyselect data=&data noprint
METHOD=srs
SEED=0
OUT=t1
SAMPSIZE=&k;
RUN;
data t1;
set t1;
n=_N_;
run;
%do i = 1 %to &k;
proc sql noprint;select x INTO :k&i from t1 where n=&i;
data t;
set t;
y&i=x-k&i;
run;
%end;
%mend a;
%a(data=t,k=3,var=x);

 

I found that I have successfully create the three sas macro variable k1 to k3 and test them using 

%put &k1.

 

However, how could I write the calcuation in sas macro. 

 

there is a problem in "y&i=x-k&i;".

 

sas cannot read k&i as macro variable. 

 

how could I resovle this?

 

thx
 

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

Hi @ffgsdf,

 

Please change the definition of the "y&i" to:

y&i=x-&&k&i;

 

Edit:

Explanation: Without the double ampersand k&i resolves to k1 in the first iteration of the %DO loop, to k2 in the second and to k3 in the third. Thus, you are seemingly referring to data step variables k1, k2, k3, which previously did not exist (log messages: "NOTE: Variable k1 is uninitialized." and "NOTE: Missing values were generated ..."). However, you want to refer to your macro variables k1, k2, k3.

 

With the corrected code, the macro processor resolves "&&" to "&" and "&i" to 1 (in the first iteration of the %DO loop) in the first pass, and then, in the second pass, &k1 to the value of macro variable k1, as desired.

View solution in original post

1 REPLY 1
FreelanceReinh
Jade | Level 19

Hi @ffgsdf,

 

Please change the definition of the "y&i" to:

y&i=x-&&k&i;

 

Edit:

Explanation: Without the double ampersand k&i resolves to k1 in the first iteration of the %DO loop, to k2 in the second and to k3 in the third. Thus, you are seemingly referring to data step variables k1, k2, k3, which previously did not exist (log messages: "NOTE: Variable k1 is uninitialized." and "NOTE: Missing values were generated ..."). However, you want to refer to your macro variables k1, k2, k3.

 

With the corrected code, the macro processor resolves "&&" to "&" and "&i" to 1 (in the first iteration of the %DO loop) in the first pass, and then, in the second pass, &k1 to the value of macro variable k1, as desired.

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

Register now!

How to choose a machine learning algorithm

Use this tutorial as a handy guide to weigh the pros and cons of these commonly used machine learning algorithms.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 1 reply
  • 1531 views
  • 0 likes
  • 2 in conversation