<?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: Assign the macro result of each obs to a new variable in datastep in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Assign-the-macro-result-of-each-obs-to-a-new-variable-in/m-p/401088#M97256</link>
    <description>&lt;P&gt;Macro variables are resolved&amp;nbsp;&lt;U&gt;before&lt;/U&gt; the data step is compiled (and then run), while code included in call execute executes&amp;nbsp;&lt;U&gt;after&lt;/U&gt;&amp;nbsp;the current step has finished, so if you expect the macro incidence_sys to set &amp;amp;obs and &amp;amp;rate, they're actually two execution stages apart.&lt;U&gt;&lt;/U&gt;&lt;/P&gt;</description>
    <pubDate>Wed, 04 Oct 2017 17:48:44 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2017-10-04T17:48:44Z</dc:date>
    <item>
      <title>Assign the macro result of each obs to a new variable in datastep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assign-the-macro-result-of-each-obs-to-a-new-variable-in/m-p/401043#M97241</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I created a macro to calculate&amp;nbsp;two values based on input and assign them to&amp;nbsp; 2 global macro variables &lt;SPAN&gt;_obs and&amp;nbsp;_rate&lt;/SPAN&gt;. But right now I have some problems when use it in a data step.&lt;/P&gt;&lt;P&gt;My data step code is the following:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data d2;&lt;BR /&gt;&amp;nbsp; set d1;&lt;BR /&gt;&amp;nbsp; call execute('%nrstr(%incidence_sys(_var1="'||Type||'"))');&lt;BR /&gt;&amp;nbsp; obs = &amp;amp;_obs;&lt;BR /&gt;&amp;nbsp; rate = &amp;amp;_rate;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The macro is&amp;nbsp;&lt;SPAN&gt;%incidence_sys.&amp;nbsp;I want to use each 'Type' of observation as my macro input. Then the macro computes and saves those two global macro variables: _obs and&amp;nbsp;_rate. Next I need to assign them to two variables of each row in dataset.&amp;nbsp; But the code seems run the macro through all rows first then assigns&amp;nbsp;the last&amp;nbsp;_obs and&amp;nbsp;_rate back to all rows.&amp;nbsp;I set my macro to put some results, it did calculate values based on each row. &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;My data set is like&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;ID&amp;nbsp; &amp;nbsp; Type ...&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;A&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;B&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;B&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;C&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I expect the result is like&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;ID&amp;nbsp; &amp;nbsp; Type&amp;nbsp; obs&amp;nbsp; rate...&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;A&amp;nbsp; &amp;nbsp; &amp;nbsp; 10&amp;nbsp; &amp;nbsp; 0.11&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;B&amp;nbsp; &amp;nbsp; &amp;nbsp; 12&amp;nbsp; &amp;nbsp; 0.51&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;B&amp;nbsp; &amp;nbsp; &amp;nbsp; 11&amp;nbsp; &amp;nbsp; 0.41&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;C&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;5&amp;nbsp; &amp;nbsp; &amp;nbsp;0.3&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;But currently my result is&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;ID&amp;nbsp; &amp;nbsp; Type&amp;nbsp; obs&amp;nbsp; rate...&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;A&amp;nbsp; &amp;nbsp; &amp;nbsp; 5&amp;nbsp; &amp;nbsp; &amp;nbsp;0.3&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;B&amp;nbsp; &amp;nbsp; &amp;nbsp; 5&amp;nbsp; &amp;nbsp; &amp;nbsp;0.3&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;B&amp;nbsp; &amp;nbsp; &amp;nbsp; 5&amp;nbsp; &amp;nbsp; &amp;nbsp;0.3&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;C&amp;nbsp; &amp;nbsp; &amp;nbsp; 5&amp;nbsp; &amp;nbsp; &amp;nbsp;0.3&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 04 Oct 2017 16:44:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assign-the-macro-result-of-each-obs-to-a-new-variable-in/m-p/401043#M97241</guid>
      <dc:creator>Lochen</dc:creator>
      <dc:date>2017-10-04T16:44:48Z</dc:date>
    </item>
    <item>
      <title>Re: Assign the macro result of each obs to a new variable in datastep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assign-the-macro-result-of-each-obs-to-a-new-variable-in/m-p/401055#M97243</link>
      <description>&lt;P&gt;Order of operations.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Call Execute executes AFTER the data step is finished, so the variables don't exist.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is PROC FCMP an option instead, since it can return values, like a function?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 04 Oct 2017 16:56:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assign-the-macro-result-of-each-obs-to-a-new-variable-in/m-p/401055#M97243</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-10-04T16:56:53Z</dc:date>
    </item>
    <item>
      <title>Re: Assign the macro result of each obs to a new variable in datastep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assign-the-macro-result-of-each-obs-to-a-new-variable-in/m-p/401056#M97244</link>
      <description>&lt;P&gt;One problem is that CALL EXECUTE doesn't do anything until a step boundary, in other words it doesn't set values in the present data step or make macro variables available in the present data step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Of course, there may be other problems as well since we can't see your data and we don't know what %incidence_sys does.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 04 Oct 2017 16:57:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assign-the-macro-result-of-each-obs-to-a-new-variable-in/m-p/401056#M97244</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2017-10-04T16:57:56Z</dc:date>
    </item>
    <item>
      <title>Re: Assign the macro result of each obs to a new variable in datastep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assign-the-macro-result-of-each-obs-to-a-new-variable-in/m-p/401060#M97246</link>
      <description>&lt;P&gt;Thanks, Reeza.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My macro does return values. But it includes some SQL procedures.&lt;/P&gt;</description>
      <pubDate>Wed, 04 Oct 2017 17:04:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assign-the-macro-result-of-each-obs-to-a-new-variable-in/m-p/401060#M97246</guid>
      <dc:creator>Lochen</dc:creator>
      <dc:date>2017-10-04T17:04:19Z</dc:date>
    </item>
    <item>
      <title>Re: Assign the macro result of each obs to a new variable in datastep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assign-the-macro-result-of-each-obs-to-a-new-variable-in/m-p/401074#M97251</link>
      <description>&lt;P&gt;To embellish upon what &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;said, you can't run PROC SQL in the middle of a DATA step.&amp;nbsp; It doesn't matter whether you hard-code the PROC SQL, generate it with macro language, or use CALL EXECUTE.&amp;nbsp; PROC SQL cannot be embedded within a DATA step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You have already provided some sample inputs.&amp;nbsp; If you were to describe your intentions&amp;nbsp;-- how the inputs should be used to calculate the outputs -- there are probably other ways to get there.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 04 Oct 2017 17:27:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assign-the-macro-result-of-each-obs-to-a-new-variable-in/m-p/401074#M97251</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-10-04T17:27:21Z</dc:date>
    </item>
    <item>
      <title>Re: Assign the macro result of each obs to a new variable in datastep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assign-the-macro-result-of-each-obs-to-a-new-variable-in/m-p/401088#M97256</link>
      <description>&lt;P&gt;Macro variables are resolved&amp;nbsp;&lt;U&gt;before&lt;/U&gt; the data step is compiled (and then run), while code included in call execute executes&amp;nbsp;&lt;U&gt;after&lt;/U&gt;&amp;nbsp;the current step has finished, so if you expect the macro incidence_sys to set &amp;amp;obs and &amp;amp;rate, they're actually two execution stages apart.&lt;U&gt;&lt;/U&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 04 Oct 2017 17:48:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assign-the-macro-result-of-each-obs-to-a-new-variable-in/m-p/401088#M97256</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-10-04T17:48:44Z</dc:date>
    </item>
    <item>
      <title>Re: Assign the macro result of each obs to a new variable in datastep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assign-the-macro-result-of-each-obs-to-a-new-variable-in/m-p/401202#M97313</link>
      <description>&lt;P&gt;Maybe you want function DOSUBL().&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro xx(var1=);
%global a ;
%if &amp;amp;var1=A %then %let a=10;
 %else %if &amp;amp;var1=B %then %let a=12;
   %else %if &amp;amp;var1=C %then %let a=18;
%mend;

data have;
input ID    Type$;
cards;
1         A
2         B
3         B
4         C
;
run;
data want;
 set have;
 rc=dosubl(cats('%xx(var1=',type,')'));
 rate=symgetn('a');
 run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 05 Oct 2017 07:08:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assign-the-macro-result-of-each-obs-to-a-new-variable-in/m-p/401202#M97313</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2017-10-05T07:08:56Z</dc:date>
    </item>
    <item>
      <title>Re: Assign the macro result of each obs to a new variable in datastep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assign-the-macro-result-of-each-obs-to-a-new-variable-in/m-p/401957#M97558</link>
      <description>&lt;P&gt;Thanks Ksharp.&lt;/P&gt;&lt;P&gt;It works and gives me what I want exactly.&lt;/P&gt;</description>
      <pubDate>Fri, 06 Oct 2017 21:20:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assign-the-macro-result-of-each-obs-to-a-new-variable-in/m-p/401957#M97558</guid>
      <dc:creator>Lochen</dc:creator>
      <dc:date>2017-10-06T21:20:50Z</dc:date>
    </item>
  </channel>
</rss>

