<?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 PROC TIMESERIES mode accumulation in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/PROC-TIMESERIES-mode-accumulation/m-p/490432#M128305</link>
    <description>&lt;P&gt;I am using PROC TIMESERIES to wrap data into a timeseries format. There is one variable in my data for which I would like to store the most frequent value. However, I could'nt find a way to do this easily in PROC TIMESERIES nor any smart work around. I was wondering whether I missed something because it appears as a rather usual problem.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data have;
	infile datalines delimiter='	';
	input customerid : 8.
		date : date9.
		variable : 3.
	;
	datalines;
2123780	11APR2017	0
2123780	13APR2017	0
2123780	16APR2017	1
2123780	16APR2017	0
2123780	16APR2017	2
2123780	16APR2017	0
2123780	21APR2017	0
2123780	23APR2017	0
2123780	23APR2017	0
2123780	26APR2017	0
2123780	29APR2017	0
2123780	29APR2017	3
2123780	01MAY2017	3
2123780	01MAY2017	2
2123780	01MAY2017	5
2123780	05MAY2017	1
2123780	07MAY2017	2
2123780	09MAY2017	2
2123780	11MAY2017	3
2123780	13MAY2017	3
2123780	14MAY2017	0
2123780	16MAY2017	2
2123780	16MAY2017	2
;
run;&lt;BR /&gt;&lt;BR /&gt;PROC TIMESERIES data=have out=want;&lt;BR /&gt;by customerID;&lt;BR /&gt;id date interval=day accumulate=???;&lt;BR /&gt;var variable;&lt;BR /&gt;run;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you in advance for any hints!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 28 Aug 2018 12:30:47 GMT</pubDate>
    <dc:creator>mat_n</dc:creator>
    <dc:date>2018-08-28T12:30:47Z</dc:date>
    <item>
      <title>PROC TIMESERIES mode accumulation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-TIMESERIES-mode-accumulation/m-p/490432#M128305</link>
      <description>&lt;P&gt;I am using PROC TIMESERIES to wrap data into a timeseries format. There is one variable in my data for which I would like to store the most frequent value. However, I could'nt find a way to do this easily in PROC TIMESERIES nor any smart work around. I was wondering whether I missed something because it appears as a rather usual problem.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data have;
	infile datalines delimiter='	';
	input customerid : 8.
		date : date9.
		variable : 3.
	;
	datalines;
2123780	11APR2017	0
2123780	13APR2017	0
2123780	16APR2017	1
2123780	16APR2017	0
2123780	16APR2017	2
2123780	16APR2017	0
2123780	21APR2017	0
2123780	23APR2017	0
2123780	23APR2017	0
2123780	26APR2017	0
2123780	29APR2017	0
2123780	29APR2017	3
2123780	01MAY2017	3
2123780	01MAY2017	2
2123780	01MAY2017	5
2123780	05MAY2017	1
2123780	07MAY2017	2
2123780	09MAY2017	2
2123780	11MAY2017	3
2123780	13MAY2017	3
2123780	14MAY2017	0
2123780	16MAY2017	2
2123780	16MAY2017	2
;
run;&lt;BR /&gt;&lt;BR /&gt;PROC TIMESERIES data=have out=want;&lt;BR /&gt;by customerID;&lt;BR /&gt;id date interval=day accumulate=???;&lt;BR /&gt;var variable;&lt;BR /&gt;run;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you in advance for any hints!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 28 Aug 2018 12:30:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-TIMESERIES-mode-accumulation/m-p/490432#M128305</guid>
      <dc:creator>mat_n</dc:creator>
      <dc:date>2018-08-28T12:30:47Z</dc:date>
    </item>
    <item>
      <title>Re: PROC TIMESERIES mode accumulation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-TIMESERIES-mode-accumulation/m-p/491145#M128735</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Unfortunately, there is no simple function that can do this that I can see. I checked around and I think these references may help. One uses SQL. If you are uncomfortable with that you will need to use something like PROC FREQ in separate operation. I suppose some other procs (Summary, Univariate, Tablulate) could work as well, but I think PROC FREQ is the best choice. &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://communities.sas.com/t5/SAS-Procedures/Most-frequent-observation/td-p/63799&amp;nbsp;&amp;nbsp;" target="_blank"&gt;https://communities.sas.com/t5/SAS-Procedures/Most-frequent-observation/td-p/63799&amp;nbsp;&amp;nbsp;&lt;/A&gt; SQL&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://www.lexjansen.com/nesug/nesug10/cc/cc16.pdf&amp;nbsp;&amp;nbsp;&amp;nbsp;" target="_blank"&gt;https://www.lexjansen.com/nesug/nesug10/cc/cc16.pdf&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/A&gt; Using Proc Freq to find most common value.&lt;/P&gt;</description>
      <pubDate>Thu, 30 Aug 2018 09:05:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-TIMESERIES-mode-accumulation/m-p/491145#M128735</guid>
      <dc:creator>Kow</dc:creator>
      <dc:date>2018-08-30T09:05:23Z</dc:date>
    </item>
    <item>
      <title>Re: PROC TIMESERIES mode accumulation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-TIMESERIES-mode-accumulation/m-p/491263#M128785</link>
      <description>&lt;P&gt;Thank you for your research you put in this topic! Since both of your links contain breaks which results in a 404 error, I am adding the correct links for other readers:&lt;/P&gt;&lt;P&gt;&lt;A href="https://communities.sas.com/t5/SAS-Procedures/Most-frequent-observation/td-p/63799" target="_self"&gt;Most frequent observation&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="https://www.lexjansen.com/nesug/nesug10/cc/cc16.pdf" target="_self"&gt;Ordering PROC FREQ Around&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Does anyone know why there is no such option implemented in most SAS procedures? In many cases, a mode seems to be a desirable information to have. I know, it has its difficulties if different values occur equally often but a siimple option in a formula should solve this problem and keep the researcher aware of what is happening.&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>Thu, 30 Aug 2018 14:30:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-TIMESERIES-mode-accumulation/m-p/491263#M128785</guid>
      <dc:creator>mat_n</dc:creator>
      <dc:date>2018-08-30T14:30:16Z</dc:date>
    </item>
    <item>
      <title>Re: PROC TIMESERIES mode accumulation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-TIMESERIES-mode-accumulation/m-p/491576#M128951</link>
      <description>&lt;P&gt;This is not exactly a solution to my original question but I want to share it anyway.&lt;/P&gt;&lt;P&gt;After performing a &lt;EM&gt;PROC TIMESERIES&lt;/EM&gt; to my data and randomly keeping the first non missing observation of a day, the following code performs a &lt;STRONG&gt;moving mode&lt;/STRONG&gt; for a specific time window.&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;%let window=7;
/*Bestimme den Mode von kategorialen Variablen*/
data want;
	set have;
	by customerID;

	/*Create an array as long as the time window*/
	array var {&amp;amp;window};
	retain var:;

	/*	Fill the array with missings for a new ID*/
	if first.customerID then
		do;
			do i = 1 to &amp;amp;window - 1;
				var{i} = .;
			end;

			/*The current observation is assigned at the last position of the array*/
			var{dim(var)} = variable;
		end;
	else
		do;
			/*If the observation is NOT then first of a new ID, then fill each array value with the one from the succeding position.*/
			do i = 1 to dim(var)-1;
				var{i} = var{i+1};
			end;

			/*The current observation is assigned at the last position of the array*/
			var{dim(var)} = variable;

			/*An array to count the most frequent value*/
			array nums {&amp;amp;window} var:;

			/*	Loop through the array and search the most frequent observation*/
			do _n_=1 to &amp;amp;window;
				count=0;

				do _i_ = _n_ to &amp;amp;window;
					/*Missing values are not considered*/
					if nums{_n_} = nums{_i_} and not  missing( nums{_n_}) then
						count + 1;
				end;

				/*A mode-value is replaced if another value occured more often. Consequently, a new mode has to beat the last mode by at least one.*/
				if count &amp;gt; maxcount then
					do;
						mode = nums{_n_};
						maxcount = count;
					end;
			end;
		end;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It is inspired by &lt;A href="https://communities.sas.com/t5/SAS-Programming/Calculate-a-moving-sum-average-depending-on-date/m-p/488888/highlight/true#M127501" target="_self"&gt;this&lt;/A&gt; and &lt;A href="https://communities.sas.com/t5/General-SAS-Programming/Finding-MODE-in-SAS-data-step/m-p/97463/highlight/true#M25163" target="_self"&gt;this&lt;/A&gt; reply on different questions and can be seen as a substitute for a &lt;EM&gt;PROC EXPAND&lt;/EM&gt; moving mode transformation.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC EXPAND DATA=have;

CONVERT variable / TRANSFORM=(MODE &amp;amp;window);

RUN;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 31 Aug 2018 09:52:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-TIMESERIES-mode-accumulation/m-p/491576#M128951</guid>
      <dc:creator>mat_n</dc:creator>
      <dc:date>2018-08-31T09:52:13Z</dc:date>
    </item>
  </channel>
</rss>

