<?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: mean of 10 variables with different starting point in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/mean-of-10-variables-with-different-starting-point/m-p/858226#M339090</link>
    <description>&lt;P&gt;easy&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":grinning_face_with_sweat:"&gt;😅&lt;/span&gt; thanks&lt;/P&gt;</description>
    <pubDate>Fri, 10 Feb 2023 10:44:39 GMT</pubDate>
    <dc:creator>Oligolas</dc:creator>
    <dc:date>2023-02-10T10:44:39Z</dc:date>
    <item>
      <title>mean of 10 variables with different starting point</title>
      <link>https://communities.sas.com/t5/SAS-Programming/mean-of-10-variables-with-different-starting-point/m-p/857952#M338988</link>
      <description>&lt;P&gt;I have 18 numerical variables pm25_total2000 to pm25_total2018&lt;/P&gt;
&lt;P&gt;Each person have a starting year between 2013 and 2018, we can call that variable "reqyear".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now I want to calculate mean for each persons 10 years before the starting year.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example if a person have starting year 2015 I want mean(of pm25_total2006-pm25_total2015)&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or if a person have starting year 2013 I want mean(of pm25_total2004-pm25_total2013)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How to do this?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 09 Feb 2023 07:24:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/mean-of-10-variables-with-different-starting-point/m-p/857952#M338988</guid>
      <dc:creator>TobbeNord</dc:creator>
      <dc:date>2023-02-09T07:24:43Z</dc:date>
    </item>
    <item>
      <title>Re: mean of 10 variables with different starting point</title>
      <link>https://communities.sas.com/t5/SAS-Programming/mean-of-10-variables-with-different-starting-point/m-p/857980#M338993</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;while performing the calculation in a datastep, I realized it became quickly diffcult to read, so I did the same using a macro step.&lt;/P&gt;
&lt;P&gt;There must be an easier way I can't think of right now&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
%** Create test data;
DATA have;
   length person $10 reqyear pm25_total2000-pm25_total2018 8;
   array pm pm25_total2000-pm25_total2018;

   do i=1 to 10;
      person=cats('Person',put(i,best.));
      do over pm;
         pm=int(ranuni(0)*100);
      end;
      reqyear=2000 + floor((1+2018-2000)*rand("uniform"));
      output;
   end;
   drop i;
RUN;


%** 1 - Using datastep;
DATA want;
   set _NULL_;
RUN;
DATA _NULL_;
   length pmname pm_start pm_end $32;
   SET have;
   ARRAY PMS pm25_total2000-pm25_total2018;
   do i=1 to dim(PMS);
      pmname    =strip(upcase(vname(PMS[i])));
      pm_start  =strip(upcase(cats('pm25_total',put(reqyear-9,best.))));
      pm_end    =strip(upcase(cats('pm25_total',put(reqyear,best.))));
   end;

   put 'N' 'OTE:' Person= reqyear= pm_start= pm_end=;
   if pm_start GE vname(PMS[1]) then do;
      call execute('DATA _hlp; LENGTH mean 8 analyzed $70; SET have(FIRSTOBS='||strip(put(_N_,best.))||' OBS='||strip(put(_N_,best.))||');');
      call execute('ARRAY pms_mean {*} '||strip(pm_start)||'--'||strip(pm_end)||';');
      call execute('mean=mean(of pms_mean[*]); analyzed="mean of '||strip(pm_start)||'--'||strip(pm_end)||'";');
      call execute('RUN;');
      call execute('DATA want; SET want _hlp; RUN; PROC DATASETS lib=work nolist; delete _:; RUN;QUIT;');
   end;
   else do;
      call execute('DATA _hlp; LENGTH mean 8  analyzed $70; SET have(FIRSTOBS='||strip(put(_N_,best.))||' OBS='||strip(put(_N_,best.))||');');
      call execute('mean=.; analyzed="mean since '||strip(pm_start)||' not calculable";');
      call execute('RUN;');
      call execute('DATA want; SET want _hlp; RUN; PROC DATASETS lib=work nolist; delete _:; RUN;QUIT;');
   end;
RUN;


%** 2 - Using Macro;
DATA want2;
   set _NULL_;
RUN;
%macro calcMean(inDS=,outDS=,n=,Miss=,startVar=,endVar=);
   %if &amp;amp;Miss. ne 1 %then %do;
      DATA _hlp; 
         LENGTH mean 8 analyzed $70; 
         SET have(FIRSTOBS=&amp;amp;n. OBS=&amp;amp;n.);
         ARRAY pms_mean {*} &amp;amp;startVar.--&amp;amp;endVar.;
         mean=mean(of pms_mean[*]); 
         analyzed="Mean of &amp;amp;startVar.--&amp;amp;endVar.";
      RUN;
   %end;
   %else %do;
      DATA _hlp; 
         LENGTH mean 8 analyzed $70; 
         SET have(FIRSTOBS=&amp;amp;n. OBS=&amp;amp;n.);
         mean=.; 
         analyzed="Mean since &amp;amp;startVar. not calculable";
      RUN;
   %end;

   DATA &amp;amp;outDS.; 
      SET &amp;amp;outDS. _hlp; 
   RUN; 

   PROC DATASETS lib=work nolist; delete _:; RUN;QUIT;
%mend calcMean;

DATA _NULL_;
   length pmname pm_start pm_end $32;
   SET have;
   ARRAY PMS pm25_total2000-pm25_total2018;
   do i=1 to dim(PMS);
      pmname    =strip(upcase(vname(PMS[i])));
      pm_start  =strip(upcase(cats('pm25_total',put(reqyear-9,best.))));
      pm_end    =strip(upcase(cats('pm25_total',put(reqyear,best.))));
   end;

   put 'N' 'OTE:' Person= reqyear= pm_start= pm_end=;
   if pm_start GE vname(PMS[1]) then 
         call execute('%nrstr(%calcMean(inDS=want,outDS=want2,n='||strip(put(_N_,best.))||'       ,startVar='||strip(pm_start)||',endVar='||strip(pm_end)||');)');
   else  call execute('%nrstr(%calcMean(inDS=want,outDS=want2,n='||strip(put(_N_,best.))||',Miss=1,startVar='||strip(pm_start)||',endVar='||strip(pm_end)||');)');
RUN;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 09 Feb 2023 10:34:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/mean-of-10-variables-with-different-starting-point/m-p/857980#M338993</guid>
      <dc:creator>Oligolas</dc:creator>
      <dc:date>2023-02-09T10:34:50Z</dc:date>
    </item>
    <item>
      <title>Re: mean of 10 variables with different starting point</title>
      <link>https://communities.sas.com/t5/SAS-Programming/mean-of-10-variables-with-different-starting-point/m-p/857985#M338994</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/166093"&gt;@TobbeNord&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I have 18 numerical variables pm25_total2000 to pm25_total2018&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;If those variables are numbered sequential there are 19 variables. If you only have 18 then you need to tell us which one is missing.&lt;/P&gt;
&lt;P&gt;If you are going to throw at problem that has 20+ variables you should provide an example data set as making us dummy of that much data is asking a bit.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is one way to get started. I am too lazy to make up that many values so I am using 10 "years" and much shorter variable names.&lt;/P&gt;
&lt;PRE&gt;data have;
  input v2000 - v2009 reqyear;
datalines;
1 2 3 4 5 6 7 8 9 10 2005
11 22 33 44 55 66 77 88 99 110 2008
;

data want;
  set have;
  array v(2000:2009) v2000-v2009;
  array t(2000:2009) _temporary_;
  /* reset as temporary arrays hold values across data step)*/
  call missing(of t(*));
  /*load temporary array*/
  do i=(reqyear-3) to reqyear;
     t[i]=v[i];
  end;
  Average= mean(of t(*));
  drop i;
run;&lt;/PRE&gt;
&lt;P&gt;The key to my approach are two arrays. One that hold the full set of values and one that we copy the values to for the mean&amp;nbsp; function to use. If you have not used arrays much you may not recognize the array statement with two values separated by a colon. That is used to indicate the low and high index values instead of the default 1 to number of elements in the array. This is often useful when you have some other variable such as your Reqyear to extract values by index.&lt;/P&gt;
&lt;P&gt;The T array is temporary, meaning that the contents are not written to the result data set. If you want to see what they might be remove the _temporary_.&lt;/P&gt;
&lt;P&gt;One thing is that while the values of _temporary_ arrays aren't written to the output data set the values are retained. So we need to start fresh with T at each step.&lt;/P&gt;
&lt;P&gt;Since I only used 10 variables I am using 3 previous "years" which is seen on the Do I= loop to extract the desired&amp;nbsp; values into the temporary array.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 09 Feb 2023 10:47:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/mean-of-10-variables-with-different-starting-point/m-p/857985#M338994</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-02-09T10:47:40Z</dc:date>
    </item>
    <item>
      <title>Re: mean of 10 variables with different starting point</title>
      <link>https://communities.sas.com/t5/SAS-Programming/mean-of-10-variables-with-different-starting-point/m-p/857994#M338999</link>
      <description>Thank you very much!</description>
      <pubDate>Thu, 09 Feb 2023 11:44:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/mean-of-10-variables-with-different-starting-point/m-p/857994#M338999</guid>
      <dc:creator>TobbeNord</dc:creator>
      <dc:date>2023-02-09T11:44:40Z</dc:date>
    </item>
    <item>
      <title>Re: mean of 10 variables with different starting point</title>
      <link>https://communities.sas.com/t5/SAS-Programming/mean-of-10-variables-with-different-starting-point/m-p/857995#M339000</link>
      <description>Wow! That was really complicated to follow. Thanks for all the effort</description>
      <pubDate>Thu, 09 Feb 2023 11:46:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/mean-of-10-variables-with-different-starting-point/m-p/857995#M339000</guid>
      <dc:creator>TobbeNord</dc:creator>
      <dc:date>2023-02-09T11:46:25Z</dc:date>
    </item>
    <item>
      <title>Re: mean of 10 variables with different starting point</title>
      <link>https://communities.sas.com/t5/SAS-Programming/mean-of-10-variables-with-different-starting-point/m-p/857998#M339001</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
call streaminit(123);
array x{*} pm25_total2000-pm25_total2018;
do id=1 to 10;
  start_year=rand('integer',2013,2018);
  do i=1 to dim(x);
    x{i}=rand('integer',1,10);
  end;
  output;
end;
drop i;
run;


data want;
 set have;
 sum=0;
 do year=start_year-9 to start_year;
   sum+vvaluex(cats('pm25_total',year));
 end;
 mean=sum/10;  
 drop sum year;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 09 Feb 2023 11:48:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/mean-of-10-variables-with-different-starting-point/m-p/857998#M339001</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2023-02-09T11:48:48Z</dc:date>
    </item>
    <item>
      <title>Re: mean of 10 variables with different starting point</title>
      <link>https://communities.sas.com/t5/SAS-Programming/mean-of-10-variables-with-different-starting-point/m-p/858032#M339015</link>
      <description>Thank you, that solution was smooth.</description>
      <pubDate>Thu, 09 Feb 2023 14:42:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/mean-of-10-variables-with-different-starting-point/m-p/858032#M339015</guid>
      <dc:creator>TobbeNord</dc:creator>
      <dc:date>2023-02-09T14:42:48Z</dc:date>
    </item>
    <item>
      <title>Re: mean of 10 variables with different starting point</title>
      <link>https://communities.sas.com/t5/SAS-Programming/mean-of-10-variables-with-different-starting-point/m-p/858226#M339090</link>
      <description>&lt;P&gt;easy&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":grinning_face_with_sweat:"&gt;😅&lt;/span&gt; thanks&lt;/P&gt;</description>
      <pubDate>Fri, 10 Feb 2023 10:44:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/mean-of-10-variables-with-different-starting-point/m-p/858226#M339090</guid>
      <dc:creator>Oligolas</dc:creator>
      <dc:date>2023-02-10T10:44:39Z</dc:date>
    </item>
  </channel>
</rss>

