Building models with SAS Enterprise Miner, SAS Factory Miner, SAS Visual Data Mining and Machine Learning or just with programming

calculate the macro variable in sas macro

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 14
Accepted Solution

calculate the macro variable in sas macro

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
 


Accepted Solutions
Solution
‎01-29-2016 10:57 AM
Trusted Advisor
Posts: 1,118

Re: calculate the macro variable in sas macro

[ Edited ]

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


All Replies
Solution
‎01-29-2016 10:57 AM
Trusted Advisor
Posts: 1,118

Re: calculate the macro variable in sas macro

[ Edited ]

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.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

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