<?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 Macro String Exclusions in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Macro-String-Exclusions/m-p/494508#M130327</link>
    <description>&lt;P&gt;Team,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let total_period=20;

%let period1=201303;
%let period2=201306;
%let period3=201309;
%let period4=201312;
%let period5=201403;
%let period6=201406;
%let period7=201409;
%let period8=201412;
%let period9=201503;
%let period10=201506;
%let period11=201509;
%let period12=201512;
%let period13=201603;
%let period14=201606;
%let period15=201609;
%let period16=201612;
%let period17=201703;
%let period18=201706;
%let period19=201709;
%let period20=201712;&lt;BR /&gt;    &lt;BR /&gt;    &lt;BR /&gt;    &lt;BR /&gt;    &lt;BR /&gt;&lt;BR /&gt;    data have;&lt;BR /&gt;input field :$8. _201303 _201306 _201309 _201312 _201403 _201406 _201409 _201412 _201503 _201506 _201509 _201512 _201603 _201606 _201609 _201612 _201703 _201706 _201709 _201712&lt;BR /&gt;;&lt;BR /&gt;datalines;&lt;BR /&gt;a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20&lt;BR /&gt;b 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20&lt;BR /&gt;c 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20&lt;BR /&gt;d 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20&lt;BR /&gt;e 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20&lt;BR /&gt;f 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20&lt;BR /&gt;;&lt;BR /&gt;run;&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to calculate mean as:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*Excluding the period 201303 here*/&lt;BR /&gt;mean_ex_201303 =
mean(_201306, 
     _201309, _201312, 
     _201403, _201406, _201409, _201412, 
     _201503, _201506, _201509, _201512,  
     _201603, _201606, _201609, _201612, 
     _201703, _201706, _201709, _201712, );&lt;BR /&gt;&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*Excluding the period 201306 here*/&lt;BR /&gt;mean_ex_201306 = mean(_201303, _201309, _201312, _201403, _201406, _201409, _201412, _201503, _201506, _201509, _201512, _201603, _201606, _201609, _201612, _201703, _201706, _201709, _201712, );&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And this has to be done for all the periods i.e.201303 to 201712(all 20 months).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please advise.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 11 Sep 2018 15:07:38 GMT</pubDate>
    <dc:creator>david27</dc:creator>
    <dc:date>2018-09-11T15:07:38Z</dc:date>
    <item>
      <title>Macro String Exclusions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-String-Exclusions/m-p/494508#M130327</link>
      <description>&lt;P&gt;Team,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let total_period=20;

%let period1=201303;
%let period2=201306;
%let period3=201309;
%let period4=201312;
%let period5=201403;
%let period6=201406;
%let period7=201409;
%let period8=201412;
%let period9=201503;
%let period10=201506;
%let period11=201509;
%let period12=201512;
%let period13=201603;
%let period14=201606;
%let period15=201609;
%let period16=201612;
%let period17=201703;
%let period18=201706;
%let period19=201709;
%let period20=201712;&lt;BR /&gt;    &lt;BR /&gt;    &lt;BR /&gt;    &lt;BR /&gt;    &lt;BR /&gt;&lt;BR /&gt;    data have;&lt;BR /&gt;input field :$8. _201303 _201306 _201309 _201312 _201403 _201406 _201409 _201412 _201503 _201506 _201509 _201512 _201603 _201606 _201609 _201612 _201703 _201706 _201709 _201712&lt;BR /&gt;;&lt;BR /&gt;datalines;&lt;BR /&gt;a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20&lt;BR /&gt;b 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20&lt;BR /&gt;c 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20&lt;BR /&gt;d 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20&lt;BR /&gt;e 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20&lt;BR /&gt;f 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20&lt;BR /&gt;;&lt;BR /&gt;run;&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to calculate mean as:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*Excluding the period 201303 here*/&lt;BR /&gt;mean_ex_201303 =
mean(_201306, 
     _201309, _201312, 
     _201403, _201406, _201409, _201412, 
     _201503, _201506, _201509, _201512,  
     _201603, _201606, _201609, _201612, 
     _201703, _201706, _201709, _201712, );&lt;BR /&gt;&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*Excluding the period 201306 here*/&lt;BR /&gt;mean_ex_201306 = mean(_201303, _201309, _201312, _201403, _201406, _201409, _201412, _201503, _201506, _201509, _201512, _201603, _201606, _201609, _201612, _201703, _201706, _201709, _201712, );&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And this has to be done for all the periods i.e.201303 to 201712(all 20 months).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please advise.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 11 Sep 2018 15:07:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-String-Exclusions/m-p/494508#M130327</guid>
      <dc:creator>david27</dc:creator>
      <dc:date>2018-09-11T15:07:38Z</dc:date>
    </item>
    <item>
      <title>Re: Macro String Exclusions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-String-Exclusions/m-p/494521#M130332</link>
      <description>&lt;P&gt;The "trick" here is to use two arrays constructed from&amp;nbsp;variable name lists, where the lists are macrovars.&amp;nbsp; You don't need all those let statements:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input field :$8. _201303 _201306 _201309 _201312 
                   _201403 _201406 _201409 _201412 
                   _201503 _201506 _201509 _201512 
                   _201603 _201606 _201609 _201612 
                   _201703 _201706 _201709 _201712;
datalines;
a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
b 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
c 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
d 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
e 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
f 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
run;

/* Make a dummy data set with just the vars of interest*/
data dummy;  
  set have;
  keep _: ;
  stop;
run;
/* Use the dictionary metadata to build 2 variables lists */
proc sql noprint;
  select name, cats('mean_ex',name) into 
     :var_list separated by ' ',
     :mean_list separated by ' '
    from dictionary.columns where memname='DUMMY' and libname='WORK'
    order by name;
quit;
%let divisor=%eval(&amp;amp;sqlobs-1);
%put &amp;amp;=mean_list;
%put &amp;amp;=var_list;
%put &amp;amp;=divisor;

/* Now make an array of values and a corresponding array for means */
data want ;
  set have;
  array values {*} &amp;amp;var_list;
  array means  {*} &amp;amp;mean_list;
  sumvalues = sum(of values{*});
  do i=1 to dim(values);
    means{i} = (sumvalues - values{i}) / &amp;amp;divisor; 
  end;
  drop sumvalues;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This program assumes you never have missing values, so the divisor is a constant (19 in your example).&amp;nbsp; If you do encounter missing vars then don't bother generating the divisor macrovar, and modify the data want step to:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Now make an array of values and a corresponding array for means */
data want ;
  set have;
  array values {*} &amp;amp;var_list;
  array means  {*} &amp;amp;mean_list;
  sumvalues=sum(of values{*});
  nvalues=n(of values{*});
  do i=1 to dim(values);
    means{i} = (sumvalues - values{i}) / (nvalues - n(values{i}));
  end;
  drop sumvalues nvalues;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 11 Sep 2018 15:54:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-String-Exclusions/m-p/494521#M130332</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2018-09-11T15:54:24Z</dc:date>
    </item>
    <item>
      <title>Re: Macro String Exclusions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-String-Exclusions/m-p/494564#M130345</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;&amp;nbsp;has indicated,&amp;nbsp;using dictionary tables&amp;nbsp;is more dynamic than hard coding &lt;FONT face="courier new,courier"&gt;%let&lt;/FONT&gt; statements. Nonetheless, the following macro should generate the statements required with the setup you've shown, for use in a data step:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro mean_period;
   %do i = 1 %to &amp;amp;total_period;
      %let p_list =;

      /* start assignment of mean value */
      mean_ex_&amp;amp;&amp;amp;period&amp;amp;i = mean(

      %do j = 1 %to &amp;amp;total_period;
         %if &amp;amp;j ne &amp;amp;i %then
            %let p_list = &amp;amp;p_list _&amp;amp;&amp;amp;period&amp;amp;j;
      %end;

      /* add commas to the list */
      %let p_list = %sysfunc(tranwrd(&amp;amp;p_list,%str( ),%str(, )));

      /* finish mean value assignment */
      &amp;amp;p_list
      );
   %end;
%mend mean_period;

options mprint;
data want;
   set have
   %mean_period;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Amir.&lt;/P&gt;</description>
      <pubDate>Tue, 11 Sep 2018 17:36:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-String-Exclusions/m-p/494564#M130345</guid>
      <dc:creator>Amir</dc:creator>
      <dc:date>2018-09-11T17:36:40Z</dc:date>
    </item>
  </channel>
</rss>

