<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: calculate the macro variable in sas macro in SAS Data Science</title>
    <link>https://communities.sas.com/t5/SAS-Data-Science/calculate-the-macro-variable-in-sas-macro/m-p/246918#M3636</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/7185"&gt;@ffgsdf﻿&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please change the definition of the "y&amp;amp;i" to:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;y&amp;amp;i=x-&amp;amp;&amp;amp;k&amp;amp;i;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Edit:&lt;/P&gt;
&lt;P&gt;Explanation: Without the double ampersand k&amp;amp;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 &lt;U&gt;data step variables&lt;/U&gt; 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 &lt;U&gt;macro variables&lt;/U&gt; k1, k2, k3.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;With the corrected code, the macro processor resolves "&amp;amp;&amp;amp;" to "&amp;amp;" and "&amp;amp;i" to 1 (in the first iteration of the %DO loop) in the first pass, and then, in the second pass, &amp;amp;k1 to the value of macro variable k1, as desired.&lt;/P&gt;</description>
    <pubDate>Fri, 29 Jan 2016 16:00:26 GMT</pubDate>
    <dc:creator>FreelanceReinh</dc:creator>
    <dc:date>2016-01-29T16:00:26Z</dc:date>
    <item>
      <title>calculate the macro variable in sas macro</title>
      <link>https://communities.sas.com/t5/SAS-Data-Science/calculate-the-macro-variable-in-sas-macro/m-p/246914#M3635</link>
      <description>&lt;P&gt;hi every one,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am try to write a k-means algorithm in one dimension data in sas macro.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;First step,&amp;nbsp;I trying to randomly sample three object from 1 to 13 and name them as k1, k2, and k3.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;second step, I want to calculate each the difference between my variable and k1 to t3 to decide which one belongs to which cluster.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, i cannot calcuate the macro variable in sas.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;the following is my code.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data t;&lt;/P&gt;
&lt;P&gt;INPUT x;&lt;BR /&gt;cards;&lt;BR /&gt;1&lt;BR /&gt;2&lt;BR /&gt;3&lt;BR /&gt;4&lt;BR /&gt;5&lt;BR /&gt;6&lt;BR /&gt;7&lt;BR /&gt;8&lt;BR /&gt;9&lt;BR /&gt;10&lt;BR /&gt;11&lt;BR /&gt;12&lt;BR /&gt;13&lt;BR /&gt;;&lt;BR /&gt;%macro a(data,k,var);&lt;BR /&gt;proc surveyselect data=&amp;amp;data noprint&lt;BR /&gt;METHOD=srs&lt;BR /&gt;SEED=0&lt;BR /&gt;OUT=t1&lt;BR /&gt;SAMPSIZE=&amp;amp;k;&lt;BR /&gt;RUN;&lt;BR /&gt;data t1;&lt;BR /&gt;set t1;&lt;BR /&gt;n=_N_;&lt;BR /&gt;run;&lt;BR /&gt;%do i = 1 %to &amp;amp;k;&lt;BR /&gt;proc sql noprint;select x INTO :k&amp;amp;i from t1 where n=&amp;amp;i;&lt;BR /&gt;data t;&lt;BR /&gt;set t;&lt;BR /&gt;y&amp;amp;i=x-k&amp;amp;i;&lt;BR /&gt;run;&lt;BR /&gt;%end;&lt;BR /&gt;%mend a;&lt;BR /&gt;%a(data=t,k=3,var=x);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I found that I have successfully create the three sas macro variable k1 to k3 and test them using&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%put &amp;amp;k1.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, how could I write the calcuation in sas macro.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;there is a problem in "&lt;SPAN&gt;y&amp;amp;i=x-k&amp;amp;i;".&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;sas cannot read k&amp;amp;i as macro variable.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;how could I resovle this?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;thx&lt;BR /&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 29 Jan 2016 15:42:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Science/calculate-the-macro-variable-in-sas-macro/m-p/246914#M3635</guid>
      <dc:creator>ffgsdf</dc:creator>
      <dc:date>2016-01-29T15:42:33Z</dc:date>
    </item>
    <item>
      <title>Re: calculate the macro variable in sas macro</title>
      <link>https://communities.sas.com/t5/SAS-Data-Science/calculate-the-macro-variable-in-sas-macro/m-p/246918#M3636</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/7185"&gt;@ffgsdf﻿&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please change the definition of the "y&amp;amp;i" to:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;y&amp;amp;i=x-&amp;amp;&amp;amp;k&amp;amp;i;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Edit:&lt;/P&gt;
&lt;P&gt;Explanation: Without the double ampersand k&amp;amp;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 &lt;U&gt;data step variables&lt;/U&gt; 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 &lt;U&gt;macro variables&lt;/U&gt; k1, k2, k3.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;With the corrected code, the macro processor resolves "&amp;amp;&amp;amp;" to "&amp;amp;" and "&amp;amp;i" to 1 (in the first iteration of the %DO loop) in the first pass, and then, in the second pass, &amp;amp;k1 to the value of macro variable k1, as desired.&lt;/P&gt;</description>
      <pubDate>Fri, 29 Jan 2016 16:00:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Science/calculate-the-macro-variable-in-sas-macro/m-p/246918#M3636</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2016-01-29T16:00:26Z</dc:date>
    </item>
  </channel>
</rss>

