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

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