<?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: Looping through variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-variables/m-p/649624#M194792</link>
    <description>&lt;PRE&gt;2472   options mprint symbolgen mlogic;
2473  %macro dothis;
2474  data rule;
2475      set test;
2476      %do i=1 %to 5;
2477      if find(results,trim("&amp;amp;&amp;amp;col&amp;amp;i..a")) then &amp;amp;&amp;amp;col&amp;amp;i=1; else &amp;amp;&amp;amp;col&amp;amp;i=0;
2478      %end;
2479  run;
2480  %mend dothis;
2481  %dothis
MLOGIC(DOTHIS):  Beginning execution.
MPRINT(DOTHIS):   data rule;
MPRINT(DOTHIS):   set test;
MLOGIC(DOTHIS):  %DO loop beginning; index variable I; start value is 1; stop value is 5; by
      value is 1.
SYMBOLGEN:  &amp;amp;&amp;amp; resolves to &amp;amp;.
SYMBOLGEN:  Macro variable I resolves to 1
SYMBOLGEN:  Macro variable COL1 resolves to DEVICE1
SYMBOLGEN:  &amp;amp;&amp;amp; resolves to &amp;amp;.
SYMBOLGEN:  Macro variable I resolves to 1
SYMBOLGEN:  Macro variable COL1 resolves to DEVICE1
MPRINT(DOTHIS):   if find(results,trim("DEVICE1a")) then DEVICE1=1;
SYMBOLGEN:  &amp;amp;&amp;amp; resolves to &amp;amp;.
SYMBOLGEN:  Macro variable I resolves to 1
SYMBOLGEN:  Macro variable COL1 resolves to DEVICE1
MPRINT(DOTHIS):   else DEVICE1=0;
MLOGIC(DOTHIS):  %DO loop index variable I is now 2; loop will iterate again.
SYMBOLGEN:  &amp;amp;&amp;amp; resolves to &amp;amp;.
SYMBOLGEN:  Macro variable I resolves to 2
SYMBOLGEN:  Macro variable COL2 resolves to DEVICE2
SYMBOLGEN:  &amp;amp;&amp;amp; resolves to &amp;amp;.
SYMBOLGEN:  Macro variable I resolves to 2
SYMBOLGEN:  Macro variable COL2 resolves to DEVICE2
MPRINT(DOTHIS):   if find(results,trim("DEVICE2a")) then DEVICE2=1;
SYMBOLGEN:  &amp;amp;&amp;amp; resolves to &amp;amp;.
SYMBOLGEN:  Macro variable I resolves to 2
SYMBOLGEN:  Macro variable COL2 resolves to DEVICE2
MPRINT(DOTHIS):   else DEVICE2=0;
MLOGIC(DOTHIS):  %DO loop index variable I is now 3; loop will iterate again.
SYMBOLGEN:  &amp;amp;&amp;amp; resolves to &amp;amp;.
SYMBOLGEN:  Macro variable I resolves to 3
SYMBOLGEN:  Macro variable COL3 resolves to DEVICE3
SYMBOLGEN:  &amp;amp;&amp;amp; resolves to &amp;amp;.
SYMBOLGEN:  Macro variable I resolves to 3
SYMBOLGEN:  Macro variable COL3 resolves to DEVICE3
MPRINT(DOTHIS):   if find(results,trim("DEVICE3a")) then DEVICE3=1;
SYMBOLGEN:  &amp;amp;&amp;amp; resolves to &amp;amp;.
SYMBOLGEN:  Macro variable I resolves to 3
SYMBOLGEN:  Macro variable COL3 resolves to DEVICE3
MPRINT(DOTHIS):   else DEVICE3=0;
MLOGIC(DOTHIS):  %DO loop index variable I is now 4; loop will iterate again.
SYMBOLGEN:  &amp;amp;&amp;amp; resolves to &amp;amp;.
SYMBOLGEN:  Macro variable I resolves to 4
SYMBOLGEN:  Macro variable COL4 resolves to DEVICES
SYMBOLGEN:  &amp;amp;&amp;amp; resolves to &amp;amp;.
SYMBOLGEN:  Macro variable I resolves to 4
SYMBOLGEN:  Macro variable COL4 resolves to DEVICES
MPRINT(DOTHIS):   if find(results,trim("DEVICESa")) then DEVICES=1;
SYMBOLGEN:  &amp;amp;&amp;amp; resolves to &amp;amp;.
SYMBOLGEN:  Macro variable I resolves to 4
SYMBOLGEN:  Macro variable COL4 resolves to DEVICES
MPRINT(DOTHIS):   else DEVICES=0;
MLOGIC(DOTHIS):  %DO loop index variable I is now 5; loop will iterate again.
SYMBOLGEN:  &amp;amp;&amp;amp; resolves to &amp;amp;.
SYMBOLGEN:  Macro variable I resolves to 5
SYMBOLGEN:  Macro variable COL5 resolves to BANGLADESH
SYMBOLGEN:  &amp;amp;&amp;amp; resolves to &amp;amp;.
SYMBOLGEN:  Macro variable I resolves to 5
SYMBOLGEN:  Macro variable COL5 resolves to BANGLADESH
MPRINT(DOTHIS):   if find(results,trim("BANGLADESHa")) then BANGLADESH=1;
SYMBOLGEN:  &amp;amp;&amp;amp; resolves to &amp;amp;.
SYMBOLGEN:  Macro variable I resolves to 5
SYMBOLGEN:  Macro variable COL5 resolves to BANGLADESH
MPRINT(DOTHIS):   else BANGLADESH=0;
MLOGIC(DOTHIS):  %DO loop index variable I is now 6; loop will not iterate again.
MPRINT(DOTHIS):   run;

NOTE: There were 244524 observations read from the data set WORK.TEST.
NOTE: The data set WORK.RULE has 244524 observations and 13 variables.
NOTE: DATA statement used (Total process time):
      real time           34.26 seconds
      cpu time            5.30 seconds


MLOGIC(DOTHIS):  Ending execution.&lt;/PRE&gt;&lt;P&gt;That's what I got. I just changed the loop to finish at 5 as to 44 would be very very long.&lt;/P&gt;</description>
    <pubDate>Fri, 22 May 2020 15:16:51 GMT</pubDate>
    <dc:creator>jepafob</dc:creator>
    <dc:date>2020-05-22T15:16:51Z</dc:date>
    <item>
      <title>Looping through variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-variables/m-p/649567#M194763</link>
      <description>&lt;P&gt;&lt;FONT&gt;Hi,&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT&gt;I have a folowing problem:&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT&gt;I have the working code below. I want to improve and shorten it.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;FONT&gt;Have:&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT&gt;data test; set test;&lt;BR /&gt;if find(results,trim("&amp;amp;col1a.")) then &amp;amp;col1.='Y'; else &amp;amp;col1.='N';&lt;BR /&gt;if find(results,trim("&amp;amp;col2a.")) then &amp;amp;col2.='Y'; else &amp;amp;col2.='N';&lt;BR /&gt;if find(results,trim("&amp;amp;col3a.")) then &amp;amp;col3.='Y'; else &amp;amp;col3.='N';&lt;BR /&gt;if find(results,trim("&amp;amp;col4a.")) then &amp;amp;col4.='Y'; else &amp;amp;col4.='N';&lt;BR /&gt;if find(results,trim("&amp;amp;col5a.")) then &amp;amp;col5.='Y'; else &amp;amp;col5.='N';&lt;BR /&gt;if find(results,trim("&amp;amp;col6a.")) then &amp;amp;col6.='Y'; else &amp;amp;col6.='N';&lt;BR /&gt;if find(results,trim("&amp;amp;col7a.")) then &amp;amp;col7.='Y'; else &amp;amp;col7.='N';&lt;BR /&gt;if find(results,trim("&amp;amp;col8a.")) then &amp;amp;col8.='Y'; else &amp;amp;col8.='N';&lt;BR /&gt;if find(results,trim("&amp;amp;col9a.")) then &amp;amp;col9.='Y'; else &amp;amp;col9.='N';&lt;BR /&gt;if find(results,trim("&amp;amp;col10a."))then &amp;amp;col10.='Y'; else &amp;amp;col10.='N'; &lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT&gt;run;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;FONT&gt;Want:&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT&gt;I am looking to replace the above with a loop to iterate through n number of variables (&amp;amp;col(x).) following the same logic.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Examples of &amp;amp;col1 and &amp;amp;col1a variables:&lt;/P&gt;&lt;P&gt;&lt;FONT&gt;2185&amp;nbsp; %put &amp;amp;col1;&lt;BR /&gt;someword&lt;BR /&gt;2186&amp;nbsp; %put &amp;amp;col1a;&lt;BR /&gt;someword=Y&lt;BR /&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT&gt;String 'results' looks like that: results='someword=Y;someword2=N;someword3=N;' etc...&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 21 May 2020 13:56:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-through-variables/m-p/649567#M194763</guid>
      <dc:creator>jepafob</dc:creator>
      <dc:date>2020-05-21T13:56:36Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-variables/m-p/649579#M194769</link>
      <description>&lt;P&gt;So you already have created macro variables &amp;amp;col1a through &amp;amp;col10a and &amp;amp;col1 to &amp;amp;col10?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;UNTESTED CODE&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro dothis;
data test1;
    set test;
    %do i=1 %to 10;
    if find(results,trim("&amp;amp;&amp;amp;col&amp;amp;i..a")) then &amp;amp;&amp;amp;col&amp;amp;i=1; else &amp;amp;&amp;amp;col&amp;amp;i=0;
    %end;
run;
%mend dothis;
%dothis&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Please notice:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The code &lt;FONT face="courier new,courier"&gt;data test; set test;&lt;/FONT&gt;&amp;nbsp;which overwrites TEST with a new TEST is not really a good approach unless you are 100% sure your code is working (and even then I usually avoid it), and thus in my first line I use&lt;FONT face="courier new,courier"&gt; data test1;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also please notice:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My preference for binary variables is 0 and 1 instead on 'N' and 'Y'. This enables you to compute percentages with a simple mean calculation.&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 21 May 2020 14:25:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-through-variables/m-p/649579#M194769</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-05-21T14:25:37Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-variables/m-p/649591#M194776</link>
      <description>&lt;P&gt;Thank you, I will test it and let you know.&lt;BR /&gt;&lt;BR /&gt;I was changing the names of the tables to more approachable. Originally it is not test and test. I put the same name by accident but thanks for pointing that out anyway &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;SPAN style="display: inline !important; float: none; background-color: #ffffff; color: #333333; font-family: Arial,Helvetica,sans-serif; font-size: 16px; font-style: normal; font-variant: normal; font-weight: 300; letter-spacing: normal; line-height: 1.7142; orphans: 2; overflow-wrap: break-word; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px;"&gt;Yes I have two loops a step before this one, that assigning all the variables for me.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="display: inline !important; float: none; background-color: #ffffff; color: #333333; font-family: Arial,Helvetica,sans-serif; font-size: 16px; font-style: normal; font-variant: normal; font-weight: 300; letter-spacing: normal; line-height: 1.7142; orphans: 2; overflow-wrap: break-word; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px;"&gt;originally is col(around 40). It varies so I am using dynamic table there.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="display: inline !important; float: none; background-color: #ffffff; color: #333333; font-family: Arial,Helvetica,sans-serif; font-size: 16px; font-style: normal; font-variant: normal; font-weight: 300; letter-spacing: normal; line-height: 1.7142; orphans: 2; overflow-wrap: break-word; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px;"&gt;So in your code instead of 10, I will have to put variable that stores how many col(x) there is. &lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 21 May 2020 14:50:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-through-variables/m-p/649591#M194776</guid>
      <dc:creator>jepafob</dc:creator>
      <dc:date>2020-05-21T14:50:34Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-variables/m-p/649600#M194780</link>
      <description>&lt;P&gt;OK so that's what I got.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT&gt;2452&amp;nbsp; %macro dothis;&lt;BR /&gt;2453&amp;nbsp; data rule;&lt;BR /&gt;2454&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; set test;&lt;BR /&gt;2455&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; %do i=1 %to &amp;amp;num_obs;&lt;BR /&gt;2456&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if find(results,trim("&amp;amp;&amp;amp;col&amp;amp;i..a")) then &amp;amp;&amp;amp;col&amp;amp;i=1; else &amp;amp;&amp;amp;col&amp;amp;i=0;&lt;BR /&gt;2457&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; %end;&lt;BR /&gt;2458&amp;nbsp; run;&lt;BR /&gt;2459&amp;nbsp; %mend dothis;&lt;BR /&gt;2460&amp;nbsp; %dothis&lt;BR /&gt;SYMBOLGEN:&amp;nbsp; Macro variable NUM_OBS resolves to 44&lt;BR /&gt;SYMBOLGEN:&amp;nbsp; &amp;amp;&amp;amp; resolves to &amp;amp;.&lt;BR /&gt;SYMBOLGEN:&amp;nbsp; Macro variable I resolves to 1&lt;BR /&gt;SYMBOLGEN:&amp;nbsp; Macro variable COL1 resolves to someword&lt;BR /&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT&gt;&lt;FONT&gt;SYMBOLGEN:&amp;nbsp; &amp;amp;&amp;amp; resolves to &amp;amp;.&lt;BR /&gt;SYMBOLGEN:&amp;nbsp; Macro variable I resolves to 1&lt;BR /&gt;SYMBOLGEN:&amp;nbsp; Macro variable COL1 resolves to someword2&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT&gt;&lt;FONT&gt;…&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT&gt;&lt;FONT&gt;and so on for all the variables up to 44.&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT&gt;&lt;FONT&gt;So after 'then' part is working perfectly, just the argument of find function is not resolving properly.&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT&gt;&lt;FONT&gt;Maybe it has something to do with this 'a' at the end. I could change it to be for example cal1 instead of col1a if that makes any difference.&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 21 May 2020 15:07:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-through-variables/m-p/649600#M194780</guid>
      <dc:creator>jepafob</dc:creator>
      <dc:date>2020-05-21T15:07:02Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-variables/m-p/649601#M194781</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options mprint symbolgen mlogic;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Use this command before your macro and then run it again and show us the whole LOG as it appears, without modification, without chopping stuff out, we need to see the code and the NOTES and WARNINGS and ERRORs for the part where&amp;nbsp;&lt;SPAN&gt;Macro variable I resolves to 1 (We don't need when I resolves to &amp;gt;1)&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Please, this is critically important, when you post the log you must preserved the formatting of the log to make sure that we can read and interpret it properly. Click on the &amp;lt;/&amp;gt; icon and then paste the log into the window that appears. DO NOT SKIP THIS STEP.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 21 May 2020 15:11:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-through-variables/m-p/649601#M194781</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-05-21T15:11:39Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-variables/m-p/649624#M194792</link>
      <description>&lt;PRE&gt;2472   options mprint symbolgen mlogic;
2473  %macro dothis;
2474  data rule;
2475      set test;
2476      %do i=1 %to 5;
2477      if find(results,trim("&amp;amp;&amp;amp;col&amp;amp;i..a")) then &amp;amp;&amp;amp;col&amp;amp;i=1; else &amp;amp;&amp;amp;col&amp;amp;i=0;
2478      %end;
2479  run;
2480  %mend dothis;
2481  %dothis
MLOGIC(DOTHIS):  Beginning execution.
MPRINT(DOTHIS):   data rule;
MPRINT(DOTHIS):   set test;
MLOGIC(DOTHIS):  %DO loop beginning; index variable I; start value is 1; stop value is 5; by
      value is 1.
SYMBOLGEN:  &amp;amp;&amp;amp; resolves to &amp;amp;.
SYMBOLGEN:  Macro variable I resolves to 1
SYMBOLGEN:  Macro variable COL1 resolves to DEVICE1
SYMBOLGEN:  &amp;amp;&amp;amp; resolves to &amp;amp;.
SYMBOLGEN:  Macro variable I resolves to 1
SYMBOLGEN:  Macro variable COL1 resolves to DEVICE1
MPRINT(DOTHIS):   if find(results,trim("DEVICE1a")) then DEVICE1=1;
SYMBOLGEN:  &amp;amp;&amp;amp; resolves to &amp;amp;.
SYMBOLGEN:  Macro variable I resolves to 1
SYMBOLGEN:  Macro variable COL1 resolves to DEVICE1
MPRINT(DOTHIS):   else DEVICE1=0;
MLOGIC(DOTHIS):  %DO loop index variable I is now 2; loop will iterate again.
SYMBOLGEN:  &amp;amp;&amp;amp; resolves to &amp;amp;.
SYMBOLGEN:  Macro variable I resolves to 2
SYMBOLGEN:  Macro variable COL2 resolves to DEVICE2
SYMBOLGEN:  &amp;amp;&amp;amp; resolves to &amp;amp;.
SYMBOLGEN:  Macro variable I resolves to 2
SYMBOLGEN:  Macro variable COL2 resolves to DEVICE2
MPRINT(DOTHIS):   if find(results,trim("DEVICE2a")) then DEVICE2=1;
SYMBOLGEN:  &amp;amp;&amp;amp; resolves to &amp;amp;.
SYMBOLGEN:  Macro variable I resolves to 2
SYMBOLGEN:  Macro variable COL2 resolves to DEVICE2
MPRINT(DOTHIS):   else DEVICE2=0;
MLOGIC(DOTHIS):  %DO loop index variable I is now 3; loop will iterate again.
SYMBOLGEN:  &amp;amp;&amp;amp; resolves to &amp;amp;.
SYMBOLGEN:  Macro variable I resolves to 3
SYMBOLGEN:  Macro variable COL3 resolves to DEVICE3
SYMBOLGEN:  &amp;amp;&amp;amp; resolves to &amp;amp;.
SYMBOLGEN:  Macro variable I resolves to 3
SYMBOLGEN:  Macro variable COL3 resolves to DEVICE3
MPRINT(DOTHIS):   if find(results,trim("DEVICE3a")) then DEVICE3=1;
SYMBOLGEN:  &amp;amp;&amp;amp; resolves to &amp;amp;.
SYMBOLGEN:  Macro variable I resolves to 3
SYMBOLGEN:  Macro variable COL3 resolves to DEVICE3
MPRINT(DOTHIS):   else DEVICE3=0;
MLOGIC(DOTHIS):  %DO loop index variable I is now 4; loop will iterate again.
SYMBOLGEN:  &amp;amp;&amp;amp; resolves to &amp;amp;.
SYMBOLGEN:  Macro variable I resolves to 4
SYMBOLGEN:  Macro variable COL4 resolves to DEVICES
SYMBOLGEN:  &amp;amp;&amp;amp; resolves to &amp;amp;.
SYMBOLGEN:  Macro variable I resolves to 4
SYMBOLGEN:  Macro variable COL4 resolves to DEVICES
MPRINT(DOTHIS):   if find(results,trim("DEVICESa")) then DEVICES=1;
SYMBOLGEN:  &amp;amp;&amp;amp; resolves to &amp;amp;.
SYMBOLGEN:  Macro variable I resolves to 4
SYMBOLGEN:  Macro variable COL4 resolves to DEVICES
MPRINT(DOTHIS):   else DEVICES=0;
MLOGIC(DOTHIS):  %DO loop index variable I is now 5; loop will iterate again.
SYMBOLGEN:  &amp;amp;&amp;amp; resolves to &amp;amp;.
SYMBOLGEN:  Macro variable I resolves to 5
SYMBOLGEN:  Macro variable COL5 resolves to BANGLADESH
SYMBOLGEN:  &amp;amp;&amp;amp; resolves to &amp;amp;.
SYMBOLGEN:  Macro variable I resolves to 5
SYMBOLGEN:  Macro variable COL5 resolves to BANGLADESH
MPRINT(DOTHIS):   if find(results,trim("BANGLADESHa")) then BANGLADESH=1;
SYMBOLGEN:  &amp;amp;&amp;amp; resolves to &amp;amp;.
SYMBOLGEN:  Macro variable I resolves to 5
SYMBOLGEN:  Macro variable COL5 resolves to BANGLADESH
MPRINT(DOTHIS):   else BANGLADESH=0;
MLOGIC(DOTHIS):  %DO loop index variable I is now 6; loop will not iterate again.
MPRINT(DOTHIS):   run;

NOTE: There were 244524 observations read from the data set WORK.TEST.
NOTE: The data set WORK.RULE has 244524 observations and 13 variables.
NOTE: DATA statement used (Total process time):
      real time           34.26 seconds
      cpu time            5.30 seconds


MLOGIC(DOTHIS):  Ending execution.&lt;/PRE&gt;&lt;P&gt;That's what I got. I just changed the loop to finish at 5 as to 44 would be very very long.&lt;/P&gt;</description>
      <pubDate>Fri, 22 May 2020 15:16:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-through-variables/m-p/649624#M194792</guid>
      <dc:creator>jepafob</dc:creator>
      <dc:date>2020-05-22T15:16:51Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-variables/m-p/649640#M194800</link>
      <description>&lt;P&gt;In the log, we see&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;if find(rule_results,trim("DEVICE1a")) then DEVICE1=1;&lt;/PRE&gt;
&lt;P&gt;If this is not what you want when the macro variable resolve, then please tell us what you do want.&lt;/P&gt;</description>
      <pubDate>Thu, 21 May 2020 17:01:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-through-variables/m-p/649640#M194800</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-05-21T17:01:24Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-variables/m-p/649664#M194805</link>
      <description>&lt;P&gt;The&lt;SPAN style="display: inline !important; float: none; background-color: #ffffff; color: #333333; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-style: normal; font-variant: normal; font-weight: 300; letter-spacing: normal; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px;"&gt; variable &amp;amp;col1a is storing in that example string 'device1=Y'&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="display: inline !important; float: none; background-color: #ffffff; color: #333333; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-style: normal; font-variant: normal; font-weight: 300; letter-spacing: normal; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px;"&gt;So it should resolve to this one: '&lt;/SPAN&gt;device1&lt;SPAN style="display: inline !important; float: none; background-color: #ffffff; color: #333333; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-style: normal; font-variant: normal; font-weight: 300; letter-spacing: normal; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px;"&gt;=Y'.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In the log it looks like it resolving variable &amp;amp;col1 which is device1 in this example and appending an 'a' at the end which is not what it should be.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So to clear it out in this example:&lt;/P&gt;&lt;P&gt;&amp;amp;col1. = 'Device1'&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;amp;col1a='Device1=Y'&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So it should resolve to:&lt;/P&gt;&lt;P&gt;&lt;SPAN style="display: inline !important; float: none; background-color: #f5f5f5; color: #333333; font-family: Menlo,Monaco,Consolas,'Courier New',monospace; font-size: 13px; font-style: normal; font-variant: normal; font-weight: 300; letter-spacing: normal; line-height: 1.7142; orphans: 2; overflow-wrap: break-word; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: pre; word-break: break-all; word-spacing: 0px;"&gt;if find(rule_results,trim("DEVICE1=Y")) then DEVICE1=1;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 21 May 2020 18:01:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-through-variables/m-p/649664#M194805</guid>
      <dc:creator>jepafob</dc:creator>
      <dc:date>2020-05-21T18:01:48Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-variables/m-p/649667#M194807</link>
      <description>&lt;P&gt;Given that you have defined the macro variables outside my ability to view them, please run this line of code and report what is written to the log.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%put &amp;amp;=col1a;&lt;/P&gt;</description>
      <pubDate>Thu, 21 May 2020 18:06:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-through-variables/m-p/649667#M194807</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-05-21T18:06:40Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-variables/m-p/649673#M194809</link>
      <description>&lt;P&gt;it is exactly what I wrote in the previous post, do you see some discrepancy somewhere?&lt;/P&gt;&lt;PRE&gt; 
2482  %put &amp;amp;=col1a;
SYMBOLGEN:  Macro variable COL1A resolves to DEVICE1=Y
COL1A=DEVICE1=Y&lt;/PRE&gt;</description>
      <pubDate>Thu, 21 May 2020 18:23:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-through-variables/m-p/649673#M194809</guid>
      <dc:creator>jepafob</dc:creator>
      <dc:date>2020-05-21T18:23:02Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-variables/m-p/649675#M194810</link>
      <description>&lt;P&gt;These things do get complicated&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Instead of&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;trim("&amp;amp;&amp;amp;col&amp;amp;i..a")&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;try removing one of the two dots&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;trim("&amp;amp;&amp;amp;col&amp;amp;i.a")&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 21 May 2020 18:28:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-through-variables/m-p/649675#M194810</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-05-21T18:28:45Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-variables/m-p/649683#M194814</link>
      <description>It looks like it is working now. It is resolving correctly. Tomorrow I will test the results and report back.&lt;BR /&gt;&lt;BR /&gt;I will be working tomorrow also on the similar pieces in this code, that could use a loop, following the similar logic and tricks you showed me here, but if I will be desperate I will post the next pieces I cannot figure out.&lt;BR /&gt;&lt;BR /&gt;Thank you very much for your help &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;BR /&gt;I am very new to all of this so did not know this double &amp;amp; or even this 'options mprint symbolgen mlogic;' which is super helpful. Thank you!</description>
      <pubDate>Thu, 21 May 2020 19:31:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-through-variables/m-p/649683#M194814</guid>
      <dc:creator>jepafob</dc:creator>
      <dc:date>2020-05-21T19:31:04Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-variables/m-p/649692#M194818</link>
      <description>&lt;P&gt;Another helpful thing is not to have macro variables that has some sort of index number that is not at the end but in the middle of the macro variable name. I am referring to &amp;amp;COL1A. The coding gets kind of confusing with &amp;amp;COL1A, as you have seen. If you made the macro variable name &amp;amp;COLA1 (yes, I know it looks like the name of a famous soft drink), the coding is much easier for &amp;amp;COLA1 &amp;amp;COLA2 &lt;EM&gt;etc&lt;/EM&gt;. than it is for &amp;amp;COL1A &amp;amp;COL2A&amp;nbsp;&lt;EM&gt;etc&lt;/EM&gt;.&lt;/P&gt;</description>
      <pubDate>Thu, 21 May 2020 20:00:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-through-variables/m-p/649692#M194818</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-05-21T20:00:07Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-variables/m-p/649716#M194827</link>
      <description>&lt;P&gt;Yeah, noticed that after your first solution. I am still learning how to name variables in practical way, very often I am lost. Thank you.&lt;/P&gt;</description>
      <pubDate>Thu, 21 May 2020 21:28:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-through-variables/m-p/649716#M194827</guid>
      <dc:creator>jepafob</dc:creator>
      <dc:date>2020-05-21T21:28:21Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-variables/m-p/649885#M194864</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;proc sql;
create table vol_val_sum as select
count(ID) as vol_aff,
sum(amount) as val_aff,
count(distinct number) as vol_a
from test
where status='Con' and(
(&amp;amp;col1. ='Y' and &amp;amp;col1 in (select mnemonic from score where score=71)) or 
(&amp;amp;col2. ='Y' and &amp;amp;col2 in (select mnemonic from score where score=71)) or 
(&amp;amp;col3. ='Y' and &amp;amp;col3 in (select mnemonic from score where score=71)) or 
(&amp;amp;col4. ='Y' and &amp;amp;col4 in (select mnemonic from score where score=71)) or 
(&amp;amp;col5. ='Y' and &amp;amp;col5 in (select mnemonic from score where score=71)) or&lt;BR /&gt;...&lt;BR /&gt;);
quit;&lt;/PRE&gt;&lt;P&gt;It looks like everything else is working from the beginning of the topic. I validated the results today so thank you very much for your help.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please see the code above. It is one of the last pieces I have a problem with as it is in proc sql and loops generally might be created in data step, right?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have cut the conditions to &amp;amp;col5. but normally they might be up to even 50.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I want is to replace those 40ish lines with something like that&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;%do  i =  1 %to &amp;amp;num_obs;&lt;/PRE&gt;&lt;P&gt;so I would not have to amend the conditions if &amp;amp;num_obs will change.&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;</description>
      <pubDate>Fri, 22 May 2020 12:27:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-through-variables/m-p/649885#M194864</guid>
      <dc:creator>jepafob</dc:creator>
      <dc:date>2020-05-22T12:27:31Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-variables/m-p/649886#M194865</link>
      <description>&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;Please see the code above. It is one of the last pieces I have a problem with as it is in proc sql and loops generally might be created in data step, right?&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;You use the word "right" and follow it with a question mark, but I just don't understand what question is.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;What I want is to replace those 40ish lines with something like that&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;%do  i =  1 %to &amp;amp;num_obs;&lt;/PRE&gt;
&lt;P&gt;so I would not have to amend the conditions if &amp;amp;num_obs will change.&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Again, it's not clear to me what the question is.&lt;/P&gt;</description>
      <pubDate>Fri, 22 May 2020 12:32:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-through-variables/m-p/649886#M194865</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-05-22T12:32:39Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-variables/m-p/649895#M194867</link>
      <description>&lt;P&gt;Sorry.&lt;/P&gt;&lt;P&gt;1. Is it possible to have loop inside where condition in proc sql?&lt;/P&gt;&lt;P&gt;2.&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;proc sql;
create table vol_val_sum as select
count(ID) as vol_aff,
sum(amount) as val_aff,
count(distinct number) as vol_a
from test
where status='Con' and(
&lt;STRONG&gt;(&amp;amp;col1. ='Y' and &amp;amp;col1 in (select mnemonic from score where score=71)) or 
(&amp;amp;col2. ='Y' and &amp;amp;col2 in (select mnemonic from score where score=71)) or 
(&amp;amp;col3. ='Y' and &amp;amp;col3 in (select mnemonic from score where score=71)) or 
(&amp;amp;col4. ='Y' and &amp;amp;col4 in (select mnemonic from score where score=71)) or 
(&amp;amp;col5. ='Y' and &amp;amp;col5 in (select mnemonic from score where score=71)) or
…
(&amp;amp;col44. ='Y' and &amp;amp;col44 in (select mnemonic from score where score=71))&lt;/STRONG&gt;
);
quit;&lt;/PRE&gt;&lt;P&gt;I want to replace the lines in where statement from &amp;amp;col1 to &amp;amp;col44 with some loop as sometimes I might have more or less than 44 variables and right now I have to remove or add new conditions to meet number of variables.&lt;/P&gt;</description>
      <pubDate>Fri, 22 May 2020 12:53:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-through-variables/m-p/649895#M194867</guid>
      <dc:creator>jepafob</dc:creator>
      <dc:date>2020-05-22T12:53:05Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-variables/m-p/649904#M194869</link>
      <description>&lt;OL&gt;
&lt;LI&gt;Macro looping is a TEXT processor, it can work inside PROC SQL or in a DATA step or in any other PROC or even outside of PROCs/outside of DATA steps, as long as it generates appropriate code. Macros can generate text anywhere in your SAS program. So if you write a macro that works inside a DATA step, that macro probably will not work inside of PROC SQL, as the code generated by the macro is not valid PROC SQL code, even if it is valid DATA step code. But you could certainly create a macro that works properly inside PROC SQL.&lt;/LI&gt;
&lt;LI&gt;How would the programmer know how many &amp;amp;COL variables there are?&lt;/LI&gt;
&lt;/OL&gt;</description>
      <pubDate>Fri, 22 May 2020 13:24:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-through-variables/m-p/649904#M194869</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-05-22T13:24:54Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-variables/m-p/649906#M194870</link>
      <description>&lt;P&gt;2. It is based on the number of rows in one table. I count the rows of that table and saving it to the variable &amp;amp;num_obs.,&lt;/P&gt;&lt;P&gt;which I use later in every loop as a indicator of maximum number of iterations.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 22 May 2020 13:43:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-through-variables/m-p/649906#M194870</guid>
      <dc:creator>jepafob</dc:creator>
      <dc:date>2020-05-22T13:43:10Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-variables/m-p/649908#M194872</link>
      <description>&lt;P&gt;The code you presented with 44 cases works on variables, not observations as you are now stating. So its not clear how &amp;amp;NUM_OBS would apply to your SQL code.&lt;/P&gt;</description>
      <pubDate>Fri, 22 May 2020 14:01:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-through-variables/m-p/649908#M194872</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-05-22T14:01:12Z</dc:date>
    </item>
  </channel>
</rss>

