<?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: iterate (through macro) in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/iterate-through-macro/m-p/671169#M201510</link>
    <description>Sounds like you're creating a Table1 for survival reporting?&lt;BR /&gt;&lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/Demographic-Table-and-Subgroup-Summary-Macro-TABLEN/ta-p/634030" target="_blank"&gt;https://communities.sas.com/t5/SAS-Communities-Library/Demographic-Table-and-Subgroup-Summary-Macro-TABLEN/ta-p/634030&lt;/A&gt;</description>
    <pubDate>Tue, 21 Jul 2020 18:23:18 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2020-07-21T18:23:18Z</dc:date>
    <item>
      <title>iterate (through macro)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/iterate-through-macro/m-p/671131#M201497</link>
      <description>&lt;P&gt;Hi,&amp;nbsp; In the process of macro building , I'm stuck at a point.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a dataset out_surv where var has values such as&lt;/P&gt;
&lt;P&gt;Var&amp;nbsp;&lt;/P&gt;
&lt;P&gt;0&lt;/P&gt;
&lt;P&gt;1&lt;/P&gt;
&lt;P&gt;3&lt;/P&gt;
&lt;P&gt;;&lt;/P&gt;
&lt;P&gt;I have created "&amp;amp;start" and "&amp;amp;end" macro variable from out_Surv. So, they have 0 and 3 subsequently.&lt;/P&gt;
&lt;P&gt;In addition to this, I've 3 other globally defined macro vars already stored for the session named &amp;amp;S0, &amp;amp;S1, &amp;amp;S3.&lt;/P&gt;
&lt;P&gt;I have to work in a dataset(named Surv which has an existing column Sep)&amp;nbsp; where,&amp;nbsp; by a help of a loop I have to iterate so that when Sep=0 then my newly created&amp;nbsp;variable named total=&amp;amp;S&lt;SPAN style="font-family: inherit;"&gt;0. similarly, when Sep=1 then total=&amp;amp;S1 and&amp;nbsp;Sep=3 then total=&amp;amp;S3&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;Surv data set looks like&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Sep&lt;/P&gt;
&lt;P&gt;0&lt;/P&gt;
&lt;P&gt;0&lt;/P&gt;
&lt;P&gt;0&lt;/P&gt;
&lt;P&gt;0&lt;/P&gt;
&lt;P&gt;1&lt;/P&gt;
&lt;P&gt;1&lt;/P&gt;
&lt;P&gt;1&lt;/P&gt;
&lt;P&gt;1&lt;/P&gt;
&lt;P&gt;3&lt;/P&gt;
&lt;P&gt;3&lt;/P&gt;
&lt;P&gt;3&lt;/P&gt;
&lt;P&gt;3&lt;/P&gt;
&lt;P&gt;I have tried this below:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
select min(var),max(var) into :min_var1, :max_var1 
from out_surv;
quit;



%do i=&amp;amp;min_var1 %to &amp;amp;max_var1
data new_dataset;
set Surv;

       %if Sep=&amp;amp;min_sepstrata. then total=&amp;amp;v0.;

          i+1;&amp;nbsp; &amp;nbsp;******this is the step where i need help*******************

run;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Kindly let me know if someone can help me with the iteration.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 21 Jul 2020 18:21:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/iterate-through-macro/m-p/671131#M201497</guid>
      <dc:creator>sahoositaram555</dc:creator>
      <dc:date>2020-07-21T18:21:03Z</dc:date>
    </item>
    <item>
      <title>Re: iterate (through macro)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/iterate-through-macro/m-p/671135#M201500</link>
      <description>&lt;P&gt;You don't seem to have provided all of the details and/or explained them clearly.&amp;nbsp; What are the undefined macro variables your code is referencing? What do they have to do with the issue?&amp;nbsp; &amp;nbsp;You talk about min and max values and use a %DO loop from the min to the max but your example data does not have every value between the min and the max (there is no 2).&amp;nbsp; Does that matter?&amp;nbsp; Will that cause an issue?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;First step is to figure out what SAS code you want to generate.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then you can think about how to use macro code to generate it.&lt;/P&gt;
&lt;P&gt;It sounds like SEP is an existing variable and you want to create TOTAL?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You could just use a series of IF statements:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if sep=0 then total=&amp;amp;s0;
if sep=1 then total=&amp;amp;s1;
....&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;That would be easy using your current %DO loop:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%do index=&amp;amp;min_var1 %to &amp;amp;max_var1 ;
  if sep=&amp;amp;index then total=&amp;amp;&amp;amp;s&amp;amp;index;
%end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 21 Jul 2020 17:33:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/iterate-through-macro/m-p/671135#M201500</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-07-21T17:33:40Z</dc:date>
    </item>
    <item>
      <title>Re: iterate (through macro)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/iterate-through-macro/m-p/671140#M201503</link>
      <description>Hi &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;, sorry about not being clear. Bunch of if statements with sep will help, but that's the purpose of creating this macro as i'm unaware of the what'll come next(instaed of 0 ,1 ,3 ) may be 1,2,4). Well, thanks for recognizing the do loop. &lt;BR /&gt;As i'm not clear, let me clear that macro vars (s0,s1,s3) contains the individual category Counts from 3 diff products. i have to use them in a dataset named Surv where their child products are present in a column named Sep. so next to each child product their parent category counts must come.lets say if the child product(sep) is 0 then total( a new column) should get the value S0, similarly for child sep=1 then parent S1 value should be in total.Minima and maxima values created for the each of their use in loop as i dont want any other observation except the ones are in out_surv which may create problems such as after 0,1, i dont want 2 to come, need 3 to enter in to the loop.&lt;BR /&gt;My trap point is while the loop is running from minima to maxima it should smoothly assign the value irrespective of  the inputs.&lt;BR /&gt;</description>
      <pubDate>Tue, 21 Jul 2020 17:55:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/iterate-through-macro/m-p/671140#M201503</guid>
      <dc:creator>sahoositaram555</dc:creator>
      <dc:date>2020-07-21T17:55:00Z</dc:date>
    </item>
    <item>
      <title>Re: iterate (through macro)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/iterate-through-macro/m-p/671163#M201506</link>
      <description>&lt;P&gt;Main point is to not put the values into macro variables to begin with. Leave them in a dataset and then just join the two.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You could just eliminate the macro code and resolve the macro variable directly in SAS code.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;total=symgetn(cats('s',sep));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want to put them into macro variables (and the list is short enough) then put them into one macro variable.&amp;nbsp; Or in your case two macro variables, one with the counts and one with the category id value.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
select category,counts
  into :categories separated by '|'
     , :cat_counts separated by '|'
from category_counts
;
%let n_categories=&amp;amp;sqlobs;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now you can loop from 1 to &amp;amp;N_CATEGORIES.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%do index=1 %to &amp;amp;n_categories;
if sep=%scan(&amp;amp;categories,&amp;amp;index,|) then total=%scan(&amp;amp;cat_counts,&amp;amp;index,|);
%end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 21 Jul 2020 18:17:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/iterate-through-macro/m-p/671163#M201506</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-07-21T18:17:36Z</dc:date>
    </item>
    <item>
      <title>Re: iterate (through macro)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/iterate-through-macro/m-p/671169#M201510</link>
      <description>Sounds like you're creating a Table1 for survival reporting?&lt;BR /&gt;&lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/Demographic-Table-and-Subgroup-Summary-Macro-TABLEN/ta-p/634030" target="_blank"&gt;https://communities.sas.com/t5/SAS-Communities-Library/Demographic-Table-and-Subgroup-Summary-Macro-TABLEN/ta-p/634030&lt;/A&gt;</description>
      <pubDate>Tue, 21 Jul 2020 18:23:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/iterate-through-macro/m-p/671169#M201510</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-07-21T18:23:18Z</dc:date>
    </item>
    <item>
      <title>Re: iterate (through macro)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/iterate-through-macro/m-p/671211#M201524</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
select min(var),max(var) into :min_var1, :max_var1 
from out_surv;
quit;

data new_dataset;
set Surv;
          %do i=&amp;amp;min_var1 %to &amp;amp;max_var1;
                         if Sep=&amp;amp;i. then total=&amp;amp;&amp;amp;s&amp;amp;i..;
         %end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Is the above what you are looking for?&lt;/P&gt;</description>
      <pubDate>Tue, 21 Jul 2020 20:50:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/iterate-through-macro/m-p/671211#M201524</guid>
      <dc:creator>smantha</dc:creator>
      <dc:date>2020-07-21T20:50:03Z</dc:date>
    </item>
    <item>
      <title>Re: iterate (through macro)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/iterate-through-macro/m-p/671279#M201553</link>
      <description>Hi &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/214340"&gt;@smantha&lt;/a&gt;,  I have this existing, the error comes while &amp;amp;&amp;amp;s&amp;amp;i resolves to S2 as it doesnot exist.&lt;BR /&gt;What i have is S0, S1,S3...that's why when the I resolves to 2 it shows below errors&lt;BR /&gt;ERROR 386-185: Expecting an arithmetic expression.&lt;BR /&gt;ERROR 200-322: The symbol is not recognized and will be ignored.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Wed, 22 Jul 2020 04:14:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/iterate-through-macro/m-p/671279#M201553</guid>
      <dc:creator>sahoositaram555</dc:creator>
      <dc:date>2020-07-22T04:14:36Z</dc:date>
    </item>
    <item>
      <title>Re: iterate (through macro)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/iterate-through-macro/m-p/671293#M201557</link>
      <description>&lt;P&gt;From your description, you do not need any macro coding at all (aside from the use of the predefined macro variables):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let s0=111;
%let s1=222;
%let s3=333;

data surv;
input Sep;
datalines;
0
0
0
0
1
1
1
1
3
3
3
3
;

data want;
set surv;
total = symget(cats('s',sep));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;It looks like it would be best for you to forget (for the time being) that macros exist. This will force you to concentrate on the data/proc step code that really does the job. See Maxim 11.&lt;/P&gt;</description>
      <pubDate>Wed, 22 Jul 2020 07:00:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/iterate-through-macro/m-p/671293#M201557</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-07-22T07:00:24Z</dc:date>
    </item>
    <item>
      <title>Re: iterate (through macro)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/iterate-through-macro/m-p/671298#M201559</link>
      <description>Thank you &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;, it's a great piece of advice.&lt;BR /&gt;</description>
      <pubDate>Wed, 22 Jul 2020 07:29:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/iterate-through-macro/m-p/671298#M201559</guid>
      <dc:creator>sahoositaram555</dc:creator>
      <dc:date>2020-07-22T07:29:16Z</dc:date>
    </item>
    <item>
      <title>Re: iterate (through macro)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/iterate-through-macro/m-p/671301#M201561</link>
      <description>&lt;P&gt;I guess you have those values for your macro variables somewhere in a dataset; if yes, creating a format from that dataset is even better in terms of code readability and performance:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data source;
input var value;
datalines;
0 111
1 222
3 333
;

data cntlin;
set source end=done;
fmtname = "lookup";
type = 'i';
rename
  var=start
  value=label
;
output;
if done
then do;
  hlo = 'O';
  value = .;
  output;
end;
run;

proc format cntlin=cntlin;
run;

data surv;
input Sep;
datalines;
0
0
0
0
1
1
1
1
2
3
3
3
3
;

data want;
set surv;
total = input(sep,lookup.);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The INPUT() function will create a numeric result; if you want your result to be character, create a numeric format (type 'N') and use PUT().&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The most advanced (and most performant) solution for a lookup is the use of hash objects:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set surv;
if _n_ = 1
then do;
  length var total 8;
  declare hash l (dataset:'source (rename=(value=total))');
  l.definekey('var');
  l.definedata('total');
  l.definedone();
end;
if l.find(key:sep) ne 0 then total = .;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bottom line: once you have created your solution and find that it has repeating code elements, or you want to make it dynamic with the use of parameters so you can apply it effortlessly to different situations, THEN you start to think about macros. NOT before that.&lt;/P&gt;</description>
      <pubDate>Wed, 22 Jul 2020 07:52:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/iterate-through-macro/m-p/671301#M201561</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-07-22T07:52:12Z</dc:date>
    </item>
  </channel>
</rss>

