<?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: Help with macros in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Help-with-macros/m-p/892155#M352394</link>
    <description>&lt;P&gt;To encapsulate this program in a macro, suitable for 3 data sets, you need to overcome a couple of problems.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;First, the output data set name is ci.&amp;nbsp; That's fine when you have one data set to process.&amp;nbsp; But if you process three data sets, each run replaces ci with a different output.&amp;nbsp; You will need 3 data set names, not 1.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Second, the code itself could use a little bit if cleaning up.&amp;nbsp; In particular:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;SUBSTR is inefficient&lt;/LI&gt;
&lt;LI&gt;Continuing to search all 25 variables is inefficient once you already have a single match that sets DMCAT to 1.&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;Try it this way:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro set_dmcat (dsn);
   data ci_&amp;amp;dsn;
      set &amp;amp;dsn;
      array icd_dx_cd_ {25};  /* autoatically uses the array name plus a numeric suffix */
      dmcat=0;
      do i=1 to 25  until (dmcat=1);
         if icd_dx_cd_{i} in: ('E08', 'E09', 'E10', 'E11', 'E13') then dmcat=1;
      end;
run;&lt;BR /&gt;%mend set_dmcat;&lt;BR /&gt;&lt;BR /&gt;%set_dmcat&amp;nbsp;(x)&lt;BR /&gt;%set_dmcat&amp;nbsp;(y)&lt;BR /&gt;%set_dmcat&amp;nbsp;(z)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Using =: compares whether two strings are equal, but makes the comparison using the number of characters in the shorter string.&amp;nbsp; That lets&amp;nbsp; you eliminate SUBSTR when you want to examine the beginning of a string.&lt;/P&gt;
&lt;P&gt;Adding the UNTIL condition lets you stop examining the rest of the diagnosis codes once a match has been found.&lt;/P&gt;</description>
    <pubDate>Thu, 31 Aug 2023 20:43:58 GMT</pubDate>
    <dc:creator>Astounding</dc:creator>
    <dc:date>2023-08-31T20:43:58Z</dc:date>
    <item>
      <title>Help with macros</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-macros/m-p/892049#M352378</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;I have trouble understanding macro.&lt;/P&gt;&lt;P&gt;I have the following code to run for a dataset x, I want a macro code for this to run in x,y, and z data sets.&lt;/P&gt;&lt;P&gt;Can anyone please help me with this?&lt;/P&gt;&lt;P&gt;Here is the code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data ci;&lt;BR /&gt;set CI16;&lt;BR /&gt;array d(25) ICD_DX_CD_1 - ICD_DX_CD_25;&lt;BR /&gt;DMCAT=0;&lt;BR /&gt;do I = 1 to 25;&lt;BR /&gt;if d(i) = SUBSTR(D{I},1,3)= 'E08' OR SUBSTR(D{I},1,3) = 'E09' OR SUBSTR(D{I},1,3)= 'E10' OR SUBSTR(D{I},1,3)= 'E11' OR SUBSTR(D{I},1,3)= 'E13'&lt;/P&gt;&lt;P&gt;then DMCAT=1;&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;Thanks a lot&lt;/P&gt;</description>
      <pubDate>Thu, 31 Aug 2023 19:28:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-macros/m-p/892049#M352378</guid>
      <dc:creator>stellapersis7</dc:creator>
      <dc:date>2023-08-31T19:28:45Z</dc:date>
    </item>
    <item>
      <title>Re: Help with macros</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-macros/m-p/892051#M352380</link>
      <description>&lt;P&gt;If I am understanding you properly&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro dothis(dataset=);
data ci;
set &amp;amp;dataset;
array d(25) ICD_DX_CD_1 - ICD_DX_CD_25;
DMCAT=0;
do I = 1 to 25;
if d(i) = SUBSTR(D{I},1,3)= 'E08' OR SUBSTR(D{I},1,3) = 'E09' OR SUBSTR(D{I},1,3)= 'E10' OR SUBSTR(D{I},1,3)= 'E11' OR SUBSTR(D{I},1,3)= 'E13'

then DMCAT=1;
end;
run;
%mend;

%dothis(dataset=x)
%dothis(dataset=y)
%dothis(dataset=z)&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 31 Aug 2023 19:32:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-macros/m-p/892051#M352380</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-08-31T19:32:23Z</dc:date>
    </item>
    <item>
      <title>Re: Help with macros</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-macros/m-p/892053#M352382</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%MACRO HelpWithMacros(inds=,outds=);
 data &amp;amp;outds.;
  set &amp;amp;inds.;
/* put the body of the datastep here */
 run;
%MEND HelpWithMacros;

options mprint symbolgen mlogic;
%HelpWithMacros(inds=work.X,outds=work.X_out);
%HelpWithMacros(inds=work.Y,outds=work.Y_out);
%HelpWithMacros(inds=work.Z,outds=work.Z_out);
/* end of program */&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;BR, Koen&lt;/P&gt;</description>
      <pubDate>Thu, 31 Aug 2023 19:37:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-macros/m-p/892053#M352382</guid>
      <dc:creator>sbxkoenk</dc:creator>
      <dc:date>2023-08-31T19:37:34Z</dc:date>
    </item>
    <item>
      <title>Re: Help with macros</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-macros/m-p/892155#M352394</link>
      <description>&lt;P&gt;To encapsulate this program in a macro, suitable for 3 data sets, you need to overcome a couple of problems.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;First, the output data set name is ci.&amp;nbsp; That's fine when you have one data set to process.&amp;nbsp; But if you process three data sets, each run replaces ci with a different output.&amp;nbsp; You will need 3 data set names, not 1.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Second, the code itself could use a little bit if cleaning up.&amp;nbsp; In particular:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;SUBSTR is inefficient&lt;/LI&gt;
&lt;LI&gt;Continuing to search all 25 variables is inefficient once you already have a single match that sets DMCAT to 1.&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;Try it this way:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro set_dmcat (dsn);
   data ci_&amp;amp;dsn;
      set &amp;amp;dsn;
      array icd_dx_cd_ {25};  /* autoatically uses the array name plus a numeric suffix */
      dmcat=0;
      do i=1 to 25  until (dmcat=1);
         if icd_dx_cd_{i} in: ('E08', 'E09', 'E10', 'E11', 'E13') then dmcat=1;
      end;
run;&lt;BR /&gt;%mend set_dmcat;&lt;BR /&gt;&lt;BR /&gt;%set_dmcat&amp;nbsp;(x)&lt;BR /&gt;%set_dmcat&amp;nbsp;(y)&lt;BR /&gt;%set_dmcat&amp;nbsp;(z)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Using =: compares whether two strings are equal, but makes the comparison using the number of characters in the shorter string.&amp;nbsp; That lets&amp;nbsp; you eliminate SUBSTR when you want to examine the beginning of a string.&lt;/P&gt;
&lt;P&gt;Adding the UNTIL condition lets you stop examining the rest of the diagnosis codes once a match has been found.&lt;/P&gt;</description>
      <pubDate>Thu, 31 Aug 2023 20:43:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-macros/m-p/892155#M352394</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2023-08-31T20:43:58Z</dc:date>
    </item>
    <item>
      <title>Re: Help with macros</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-macros/m-p/892563#M352523</link>
      <description>&lt;P&gt;Hi sbxkoenk&lt;/P&gt;&lt;P&gt;my new code for 3 datasets was :&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;STRONG&gt;%MACRO&lt;/STRONG&gt;&lt;/SPAN&gt; HW2_DX(inds=,outds=);&lt;/P&gt;&lt;P&gt;data &amp;amp;&lt;SPAN&gt;outds.&lt;/SPAN&gt;;&lt;/P&gt;&lt;P&gt;&lt;SPAN class=""&gt;&amp;nbsp;&lt;/SPAN&gt;set &amp;amp;&lt;SPAN&gt;inds.&lt;/SPAN&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN class=""&gt;&amp;nbsp; &lt;/SPAN&gt;array d(&lt;SPAN&gt;&lt;STRONG&gt;26&lt;/STRONG&gt;&lt;/SPAN&gt;) PRMRY_DX_CD ICD_DX_CD_1 - ICD_DX_CD_25;&lt;/P&gt;&lt;P&gt;DMCAT=&lt;SPAN&gt;&lt;STRONG&gt;0&lt;/STRONG&gt;&lt;/SPAN&gt;;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;%do&lt;/SPAN&gt; I = &lt;SPAN&gt;&lt;STRONG&gt;1&lt;/STRONG&gt;&lt;/SPAN&gt; &lt;SPAN&gt;%to&lt;/SPAN&gt; &lt;SPAN&gt;&lt;STRONG&gt;26&lt;/STRONG&gt;&lt;/SPAN&gt;;&lt;/P&gt;&lt;P&gt;if d(i) = SUBSTR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en mlogic&lt;SPAN&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;%&lt;STRONG&gt;&lt;I&gt;HW2_DX&lt;/I&gt;&lt;/STRONG&gt;(inds=hw2j.syhdr_commercial_inpatient_2016,outds=hw2_1);&lt;/P&gt;&lt;P&gt;%&lt;STRONG&gt;&lt;I&gt;HW2_DX&lt;/I&gt;&lt;/STRONG&gt;(inds=hw2j.syhdr_commercial_outpatient_2016,outds=hw2_2);&lt;/P&gt;&lt;P&gt;%&lt;STRONG&gt;&lt;I&gt;HW2_DX&lt;/I&gt;&lt;/STRONG&gt;(inds=hw2j.syhdr_medicaid_inpatient_2016,outds=hw2_3);&lt;/P&gt;&lt;P&gt;This does not work and gives an error of "variable I is uninitialized"&lt;/P&gt;&lt;P&gt;need some help!&lt;/P&gt;</description>
      <pubDate>Mon, 04 Sep 2023 14:36:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-macros/m-p/892563#M352523</guid>
      <dc:creator>stellapersis7</dc:creator>
      <dc:date>2023-09-04T14:36:47Z</dc:date>
    </item>
    <item>
      <title>Re: Help with macros</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-macros/m-p/892572#M352530</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There is NO variable in your dataset with the name "i".&lt;BR /&gt;There is only a macro variable with the name "i".&lt;BR /&gt;If you want to resolve the macro variable (to get its value), you need to specify &amp;amp;i. .&lt;/P&gt;
&lt;P&gt;(the dot after the macro variable name is only needed if "confusion" is possible)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You don't need quotes here (as you deal with a numeric value)&lt;/P&gt;
&lt;P&gt;, but in case you need quotes (for a string value) , just remember this :&lt;BR /&gt;Macro variables in SAS won't resolve when they are in single quotes, like '&amp;amp;myvar'&amp;nbsp;. They need to be in double quotes, "&amp;amp;myvar" , in order to resolve properly.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;BR, Koen&lt;/P&gt;</description>
      <pubDate>Mon, 04 Sep 2023 15:14:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-macros/m-p/892572#M352530</guid>
      <dc:creator>sbxkoenk</dc:creator>
      <dc:date>2023-09-04T15:14:58Z</dc:date>
    </item>
    <item>
      <title>Re: Help with macros</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-macros/m-p/892574#M352532</link>
      <description>&lt;P&gt;%DO works on macro variables, such as &amp;amp;i. DO works on data step variables, such as i.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In your code, use DO instead of %DO (and TO instead of %TO)&lt;/P&gt;</description>
      <pubDate>Mon, 04 Sep 2023 17:10:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-macros/m-p/892574#M352532</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-09-04T17:10:34Z</dc:date>
    </item>
  </channel>
</rss>

