<?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: Macro/Array for calculating averages on multiple, repeated variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Macro-Array-for-calculating-averages-on-multiple-repeated/m-p/403260#M278907</link>
    <description>&lt;P&gt;I'm thinking a macro is not needed here.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Append&amp;nbsp;all your data into one data set, with a YEAR variable to indicate 6, 12, 18, 24. Then compute the&amp;nbsp;n for each individual and each measurement. This can be done in a data step. If n=1 then in the data step, then the mean is missing. If n=2 or n=3, then (for example regarding height)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;height_mean = mean(of height1-height3);&lt;/PRE&gt;</description>
    <pubDate>Wed, 11 Oct 2017 17:05:32 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2017-10-11T17:05:32Z</dc:date>
    <item>
      <title>Macro/Array for calculating averages on multiple, repeated variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-Array-for-calculating-averages-on-multiple-repeated/m-p/403247#M278905</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm working with four data sets (6-year, 12-year, 18-year and 24-year measurements), within each data set&amp;nbsp;every observation is unique to&amp;nbsp;a person.&amp;nbsp; &amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In each of these data sets, there are 13 measures of interest (height, weight, pulse....) that were measured 3 times (height1, height2, height3) there are cases where an individual only had 1 or 2 measurements (i.e. height1=48, height2=48.2, height3= . ).&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm looking to find the average measurement on cases where the individual had 2 or more measurements (for all 13 variables of interest).&amp;nbsp; My base code works (see below) but&amp;nbsp;I'd like to create a macro so I don't have 26 IF/DO statements&amp;nbsp; x 4 data sets.&amp;nbsp; &amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any Suggestions on how to shorten up the code?&amp;nbsp; Any help is greatly appreciated!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;%LET WT= WEIGHT;
%LET HT= HEIGHT;
.
.
.
.


DATA Y;
SET X;
IF &amp;amp;WT.1 NE . AND &amp;amp;WT.2 NE . AND &amp;amp;WT.3 NE . THEN DO;
AVG&amp;amp;WT. = (&amp;amp;WT.1+&amp;amp;WT.2+&amp;amp;WT.3)/3;
END;

IF &amp;amp;WT.1 NE . AND &amp;amp;WT.2 NE . AND &amp;amp;WT.3 = . THEN DO;
AVG&amp;amp;WT. = (&amp;amp;WT.1+&amp;amp;WT.2)/2;
END;

IF &amp;amp;HT.1 NE........

RUN;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 11 Oct 2017 16:40:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-Array-for-calculating-averages-on-multiple-repeated/m-p/403247#M278905</guid>
      <dc:creator>glcoolj12</dc:creator>
      <dc:date>2017-10-11T16:40:20Z</dc:date>
    </item>
    <item>
      <title>Re: Macro/Array for calculating averages on multiple, repeated variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-Array-for-calculating-averages-on-multiple-repeated/m-p/403254#M278906</link>
      <description>&lt;P&gt;It sounds like you want to calculate the mean only if there are at least two values?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if n(of weight1-weight3) &amp;gt;= 2 then weight_avg=mean(of weight1-weight3);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I am not sure that macro code is much use in this situation.&lt;/P&gt;
&lt;P&gt;Just replicate the line and change the variable names.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But if you wanted you could create a macro to take in a list of base names and generate the above code.&amp;nbsp; You could even make the minimum number of values and number of replicates parameters.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro atleast(list,n=3,min=2);
%local i base;
%do i=1 %to %sysfunc(countw(&amp;amp;list));
%let base=%scan(&amp;amp;list,&amp;amp;i);
if n(of &amp;amp;base.1-&amp;amp;base.&amp;amp;n) &amp;gt;= &amp;amp;min) then &amp;amp;base._avg=mean(of &amp;amp;base.1-&amp;amp;base.&amp;amp;n);
%end;
%mend atleast;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then you could use it for HEIGHT and WEIGHT;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have ;
%atleast(height weight)
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 11 Oct 2017 16:54:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-Array-for-calculating-averages-on-multiple-repeated/m-p/403254#M278906</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-10-11T16:54:14Z</dc:date>
    </item>
    <item>
      <title>Re: Macro/Array for calculating averages on multiple, repeated variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-Array-for-calculating-averages-on-multiple-repeated/m-p/403260#M278907</link>
      <description>&lt;P&gt;I'm thinking a macro is not needed here.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Append&amp;nbsp;all your data into one data set, with a YEAR variable to indicate 6, 12, 18, 24. Then compute the&amp;nbsp;n for each individual and each measurement. This can be done in a data step. If n=1 then in the data step, then the mean is missing. If n=2 or n=3, then (for example regarding height)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;height_mean = mean(of height1-height3);&lt;/PRE&gt;</description>
      <pubDate>Wed, 11 Oct 2017 17:05:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-Array-for-calculating-averages-on-multiple-repeated/m-p/403260#M278907</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2017-10-11T17:05:32Z</dc:date>
    </item>
    <item>
      <title>Re: Macro/Array for calculating averages on multiple, repeated variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-Array-for-calculating-averages-on-multiple-repeated/m-p/403277#M278908</link>
      <description>&lt;P&gt;On top of &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;'s suggestion, If your variables of interest share some traits, such as they are at least 2 with the same prefix (shown in the example code ), or all variables except 'ID'/'Group' etc.(not shown), &amp;nbsp;you could use some variants of the following to save some typing:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  
%macro atleast(list,min=2);
%local i base;
%do i=1 %to %sysfunc(countw(&amp;amp;list));
%let base=%scan(&amp;amp;list,&amp;amp;i);
if n(of &amp;amp;base.:) &amp;gt;= &amp;amp;min then &amp;amp;base._avg=mean(of &amp;amp;base.:);
%end;
%mend atleast;


proc sql;
select DISTINCT PRXCHANGE('s/\d*$//',1,strip(name))  into :list separated by ' ' from dictionary.columns 
where libname='WORK' AND MEMNAME='HAVE'
group by PRXCHANGE('s/\d*$//',1,strip(name))
having count(*) &amp;gt;=2
;
QUIT;


data want;
  set have ;
%atleast(&amp;amp;list.)
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 11 Oct 2017 17:40:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-Array-for-calculating-averages-on-multiple-repeated/m-p/403277#M278908</guid>
      <dc:creator>Haikuo</dc:creator>
      <dc:date>2017-10-11T17:40:42Z</dc:date>
    </item>
    <item>
      <title>Re: Macro/Array for calculating averages on multiple, repeated variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-Array-for-calculating-averages-on-multiple-repeated/m-p/403328#M278909</link>
      <description>&lt;P&gt;Thank you to everyone for their very helpful assistance!&lt;/P&gt;</description>
      <pubDate>Wed, 11 Oct 2017 19:28:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-Array-for-calculating-averages-on-multiple-repeated/m-p/403328#M278909</guid>
      <dc:creator>glcoolj12</dc:creator>
      <dc:date>2017-10-11T19:28:07Z</dc:date>
    </item>
  </channel>
</rss>

