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-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

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
  • 1776 views
  • 0 likes
  • 2 in conversation