<?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: SAS DO Loop Macro in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/SAS-DO-Loop-Macro/m-p/694869#M211956</link>
    <description>&lt;P&gt;So you aim to see in which cities an individual has customers?&lt;/P&gt;</description>
    <pubDate>Wed, 28 Oct 2020 14:14:45 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2020-10-28T14:14:45Z</dc:date>
    <item>
      <title>SAS DO Loop Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-DO-Loop-Macro/m-p/694865#M211953</link>
      <description>&lt;P&gt;I have a dataset on which I am going to be performing a calculation. The calculation will need to be performed for each individual.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The structure of the data is similar to&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data app;
input ID $ A1_CITY $ A2_CITY &amp;amp; NUM_OF_CUSTS
datalines;
001 LONDON LONDON 2
002 LONDON LEEDS 2
003 MANCHESTER LONDON 2
004 LEEDS LONDON 2
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;As the calculation is quite long I was thinking of using a do loop within a macro for this. This is what I have come up with...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data calc;
set app;

%let I=1;

%macro calc;
%do %while (&amp;amp;I &amp;lt;= NUM_OF_CUSTS);
%if A&amp;amp;i._CITY = "LONDON" %THEN LONDON = "Y"; %else LONDON = "N"

/*rest of calc here*/&lt;BR /&gt;/*%if London = "Y" %then... %else...*/

%let I = %eval(&amp;amp;I+1);
%end;
%mend;
run;

%calc;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;the statement&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%do %while (&amp;amp;I &amp;lt;= NUM_OF_CUSTS);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;also isn't being resolved as expected for some reason&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help as to where I'm going wrong would be greatly appreciated&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 28 Oct 2020 14:09:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-DO-Loop-Macro/m-p/694865#M211953</guid>
      <dc:creator>twenty7</dc:creator>
      <dc:date>2020-10-28T14:09:30Z</dc:date>
    </item>
    <item>
      <title>Re: SAS DO Loop Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-DO-Loop-Macro/m-p/694868#M211955</link>
      <description>&lt;P&gt;Macro code is used to generate SAS code.&amp;nbsp; So first show what SAS code you want to generate and then explain any pattern to the code generation.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It is hard to tell from your description what you want to do but it does not look like it needs any macro code at all.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data calc;
  set app;
  if a1_city = 'LONDON' or a2_city='LONDON' then london='Y';
  else london='N';
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 28 Oct 2020 14:14:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-DO-Loop-Macro/m-p/694868#M211955</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-10-28T14:14:44Z</dc:date>
    </item>
    <item>
      <title>Re: SAS DO Loop Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-DO-Loop-Macro/m-p/694869#M211956</link>
      <description>&lt;P&gt;So you aim to see in which cities an individual has customers?&lt;/P&gt;</description>
      <pubDate>Wed, 28 Oct 2020 14:14:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-DO-Loop-Macro/m-p/694869#M211956</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-10-28T14:14:45Z</dc:date>
    </item>
    <item>
      <title>Re: SAS DO Loop Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-DO-Loop-Macro/m-p/694883#M211964</link>
      <description>&lt;P&gt;so essentially I want to perform a different calculation based on the city in which that particular applicant resides. Currently there are only two calculations - one for 'London' and for 'not London' but eventually there could be a calculation for each city.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Due to the number of potential calculations I wanted to be able code each calculation once and then loop through it for each applicant.&lt;/P&gt;</description>
      <pubDate>Wed, 28 Oct 2020 14:36:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-DO-Loop-Macro/m-p/694883#M211964</guid>
      <dc:creator>twenty7</dc:creator>
      <dc:date>2020-10-28T14:36:28Z</dc:date>
    </item>
    <item>
      <title>Re: SAS DO Loop Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-DO-Loop-Macro/m-p/694887#M211966</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data app;
    length A1_CITY A2_CITY $20.;
    input ID $ A1_CITY $ A2_CITY $ NUM_OF_CUSTS;
    datalines;
001 LONDON LONDON 2
002 LONDON LEEDS 2
003 MANCHESTER LONDON 2
004 LEEDS LONDON 2
;
run;

data want;
    set app;
    array cities(3) $1 LONDON LEEDS MANCHESTER;
    array A_CITY A1_CITY A2_CITY;

    do i=1 to dim(cities);
        if vname(cities(i)) in A_CITY then cities(i)="Y";
        else cities(i)="N";
    end;

	drop i;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 28 Oct 2020 14:42:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-DO-Loop-Macro/m-p/694887#M211966</guid>
      <dc:creator>gamotte</dc:creator>
      <dc:date>2020-10-28T14:42:42Z</dc:date>
    </item>
    <item>
      <title>Re: SAS DO Loop Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-DO-Loop-Macro/m-p/694888#M211967</link>
      <description>&lt;P&gt;The data step already does that loop, by working through the dataset (where you have one observation per applicant, as I see it) one observation after the other.&lt;/P&gt;</description>
      <pubDate>Wed, 28 Oct 2020 14:42:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-DO-Loop-Macro/m-p/694888#M211967</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-10-28T14:42:00Z</dc:date>
    </item>
    <item>
      <title>Re: SAS DO Loop Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-DO-Loop-Macro/m-p/694895#M211969</link>
      <description>&lt;P&gt;It seems that you haven't really grabbed the main concepts of SAS macros.&lt;/P&gt;
&lt;P&gt;Macros only generate text substitutions and are handled prior to the&lt;/P&gt;
&lt;P&gt;program execution. Hence, they have no knowledge of what is inside SAS&lt;/P&gt;
&lt;P&gt;datasets.&lt;/P&gt;
&lt;P&gt;Thus, the instruction&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;%do %while (&amp;amp;I &amp;lt;= NUM_OF_CUSTS);&lt;/LI-CODE&gt;
&lt;P&gt;makes no sense as &amp;amp;i. is an integer which is compared to the string "NUM_OF_CUSTS".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, while defining a macro inside a datastep is allowed :&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
set sashelp.class;

%macro a;
   put age;
%mend;

%a

run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;it doesn't make sense to do so as the macro is defined globally (independently&lt;/P&gt;
&lt;P&gt;of the datastep), so doing so only obfuscates the code.&lt;/P&gt;</description>
      <pubDate>Wed, 28 Oct 2020 14:56:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-DO-Loop-Macro/m-p/694895#M211969</guid>
      <dc:creator>gamotte</dc:creator>
      <dc:date>2020-10-28T14:56:33Z</dc:date>
    </item>
    <item>
      <title>Re: SAS DO Loop Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-DO-Loop-Macro/m-p/694898#M211970</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/91983"&gt;@twenty7&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;so essentially I want to perform a different calculation based on the city in which that particular applicant resides. Currently there are only two calculations - one for 'London' and for 'not London' but eventually there could be a calculation for each city.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Due to the number of potential calculations I wanted to be able code each calculation once and then loop through it for each applicant.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Sounds like an &lt;A href="http://xyproblem.info/" target="_self"&gt;XY problem&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;Please explain the larger context of what you are trying to do to get a better solution.&amp;nbsp; Provide example output data for the given input data.&lt;/P&gt;</description>
      <pubDate>Wed, 28 Oct 2020 15:09:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-DO-Loop-Macro/m-p/694898#M211970</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-10-28T15:09:14Z</dc:date>
    </item>
    <item>
      <title>Re: SAS DO Loop Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-DO-Loop-Macro/m-p/694911#M211979</link>
      <description>&lt;P&gt;I need to be able to calculate the expenditure for each person on an application. The expenditure calculation is different based on the city in which the person resides. In the data I have available I am able to determine the number of persons on the application and the city. The maximum number of persons on an application is 3.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;the current solution I have is&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if a1_city = London then a1_affordability_calc = 'a' else 
if a1_city in (Manchester, Liverpool) then a1_affordability_calc = 'b' else 
if a1_city in (Birmingham) then a1_affordability_calc = 'c' else 
if a1_city in (Glasgow) then a1_affordability_calc = 'd' else 
a1_affordability_calc = 'e';

if a1_affordability_calc = 'a' then a1_calc_result = (x+y+z)*0.25;
if a1_affordability_calc = 'b' then a1_calc_result = (x+y+z)*0.10;
if a1_affordability_calc = 'c' then a1_calc_result = (x+y+z)*0.21;
if a1_affordability_calc = 'd' then a1_calc_result = (x+y+z)*0.23;
if a1_affordability_calc = 'e' then a1_calc_result = (x+y+z)*0.20;&lt;BR /&gt;&lt;BR /&gt;/*REPEAT ABOVE REPLACING THE PREFIX a1 with a2 and a3*/&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The calculations will be a lot more complicated but hopefully this explains the problem better&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I wanted to avoid in my potential solution is repeating the same section of code with the calculations for each of the 3 potential applicants.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 28 Oct 2020 15:34:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-DO-Loop-Macro/m-p/694911#M211979</guid>
      <dc:creator>twenty7</dc:creator>
      <dc:date>2020-10-28T15:34:57Z</dc:date>
    </item>
    <item>
      <title>Re: SAS DO Loop Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-DO-Loop-Macro/m-p/694922#M211987</link>
      <description>&lt;P&gt;A data step operates on every observation.&amp;nbsp; So if by&amp;nbsp;&lt;SPAN&gt;applicant you mean observation then you don't have to do anything for that.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;If by APPLICANT you mean the 1,2,3 that you have coded into the variable names then perhaps you should just first transpose the data.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data app;
  input ID $ NUM_OF_CUSTS @;
  do applicant=1 to num_of_custs ;
     input city &amp;amp;:$20. @;
     output;
  end;
datalines;
001 2 LONDON LONDON 
002 2 LONDON LEEDS 
003 2 MANCHESTER LONDON 
004 2 LEEDS LONDON 
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN style="font-family: inherit;"&gt;Otherwise use an ARRAY so that you can use the same code for every variable.&amp;nbsp; Example (notice how much easier it is when you put the number at the END of the name):&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;array city city1 - city3 ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;To assign a grouping based on value of the CITY variable you might want to use a format.&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format ;
value $calc_type 
  'London' = 'a' 
  'Manchester', 'Liverpool' = 'b'
  'Birmingham' = 'c'
  'Glasgow' = 'd'
  other = 'e'
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;Now your calculation code is simple.&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set app;
  select (put(city,$calc_type.));
    when ('a')  calc_result = (x+y+z)*0.25;
    when ('b')  calc_result = (x+y+z)*0.10;
    when ('c')  calc_result = (x+y+z)*0.21;
    when ('d')  calc_result = (x+y+z)*0.23;
    when ('e')  calc_result = (x+y+z)*0.20;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;There is probably even more you could do to simplify.&lt;/P&gt;
&lt;P&gt;Notice that macro code is not needed.&lt;/P&gt;</description>
      <pubDate>Wed, 28 Oct 2020 15:57:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-DO-Loop-Macro/m-p/694922#M211987</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-10-28T15:57:51Z</dc:date>
    </item>
    <item>
      <title>Re: SAS DO Loop Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-DO-Loop-Macro/m-p/694927#M211989</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data app;
    length A1_CITY A2_CITY $20.;
    input ID $ A1_CITY $ A2_CITY $ NUM_OF_CUSTS;
    datalines;
001 LONDON LONDON 2
002 LONDON LEEDS 2
003 MANCHESTER LONDON 2
004 LEEDS LONDON 2
;
run;

data want;
    set app;
    array cities $1 LONDON LEEDS MANCHESTER BIRMINGHAM GLASGOW;
    array A_CITY A1_CITY A2_CITY;
    array afford $1. a1_affordability_calc a2_affordability_calc;
    array increment(5) _TEMPORARY_ (0.25 0.10 0.21 0.23 0.20);
    array calc $20. a1_calc_result a2_calc_result;

    do i=1 to dim(cities);
        cities(i)=byte(96+i); /* a, b, c, ... */
    end;

    do j=1 to dim(A_CITY);
        do i=1 to dim(cities);
            if A_CITY(j)=vname(cities(i)) then do;
                afford(j)=cities(i);
                leave;
            end;
        end;

        if i le dim(cities) then calc(j)=cats("(x+y+z)+", increment(i));
    end;

	drop i j;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 28 Oct 2020 16:00:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-DO-Loop-Macro/m-p/694927#M211989</guid>
      <dc:creator>gamotte</dc:creator>
      <dc:date>2020-10-28T16:00:29Z</dc:date>
    </item>
  </channel>
</rss>

