<?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: change time series data into range in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/change-time-series-data-into-range/m-p/559002#M156060</link>
    <description>&lt;PRE&gt;proc sort data=table1; by product date;
run;

data want;
set table1;
by product date price notsorted;
retain start_date;

if first.price then start_date=date;

if last.price then do;
    end_date = date; 
    output;
end;

drop date;
run;

&lt;/PRE&gt;
&lt;P&gt;Use the NOTSORTED option to identify changes in prices.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Use the RETAIN to hold the start_date the same until it ends.&lt;/P&gt;
&lt;P&gt;Use OUTPUT to explicitly output at the end of a series.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/200312"&gt;@Srigyan&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I have a following time series data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;Product	DATE	price
A	01-Jan-18	10
A	02-Jan-18	10
A	03-Jan-18	10
A	04-Jan-18	10
A	05-Jan-18	10
A	06-Jan-18	10
A	07-Jan-18	3
A	08-Jan-18	11
A	10-Jan-18	11
B	15-Jan-18	15
B	16-Jan-17	15
B	17-Jan-17	15
B	20-Jan-17	15
B	21-Jan-17	19
B	22-Jan-17	19
B	23-Jan-17	19
B	24-Jan-17	18&lt;/PRE&gt;
&lt;P&gt;code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Table1;

input product $ date date9.  price;
format date date9.;
datalines;
A 01-Jan-18 10
A 02-Jan-18 10
A 03-Jan-18 10
A 04-Jan-18 10
A 05-Jan-18 10
A 06-Jan-18 10
A 07-Jan-18 3
A 08-Jan-18 11
A 10-Jan-18 11
B 15-Jan-18 15
B 16-Jan-17 15
B 17-Jan-17 15
B 20-Jan-17 15
B 21-Jan-17 19
B 22-Jan-17 19
B 23-Jan-17 19
B 24-Jan-17 18
;

run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I wanted to have this data converted in to range.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;Product	Start_date	End_date	Price
A	01-Jan-18	06-Jan-18	10
A	07-Jan-18	07-Jan-18	3
A	08-Jan-18	08-Jan-18	11
A	10-Jan-18	10-Jan-18	11
B	15-Jan-18	17-Jan-17	15
B	20-Jan-17	20-Jan-17	15
B	21-Jan-17	23-Jan-17	19
B	24-Jan-17	24-Jan-17	18&lt;/PRE&gt;
&lt;P&gt;Please help...&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 15 May 2019 15:14:07 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2019-05-15T15:14:07Z</dc:date>
    <item>
      <title>change time series data into range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/change-time-series-data-into-range/m-p/558968#M156045</link>
      <description>&lt;P&gt;I have a following time series data.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;Product	DATE	price
A	01-Jan-18	10
A	02-Jan-18	10
A	03-Jan-18	10
A	04-Jan-18	10
A	05-Jan-18	10
A	06-Jan-18	10
A	07-Jan-18	3
A	08-Jan-18	11
A	10-Jan-18	11
B	15-Jan-18	15
B	16-Jan-17	15
B	17-Jan-17	15
B	20-Jan-17	15
B	21-Jan-17	19
B	22-Jan-17	19
B	23-Jan-17	19
B	24-Jan-17	18&lt;/PRE&gt;&lt;P&gt;code:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Table1;

input product $ date date9.  price;
format date date9.;
datalines;
A 01-Jan-18 10
A 02-Jan-18 10
A 03-Jan-18 10
A 04-Jan-18 10
A 05-Jan-18 10
A 06-Jan-18 10
A 07-Jan-18 3
A 08-Jan-18 11
A 10-Jan-18 11
B 15-Jan-18 15
B 16-Jan-17 15
B 17-Jan-17 15
B 20-Jan-17 15
B 21-Jan-17 19
B 22-Jan-17 19
B 23-Jan-17 19
B 24-Jan-17 18
;

run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I wanted to have this data converted in to range.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;Product	Start_date	End_date	Price
A	01-Jan-18	06-Jan-18	10
A	07-Jan-18	07-Jan-18	3
A	08-Jan-18	08-Jan-18	11
A	10-Jan-18	10-Jan-18	11
B	15-Jan-18	17-Jan-17	15
B	20-Jan-17	20-Jan-17	15
B	21-Jan-17	23-Jan-17	19
B	24-Jan-17	24-Jan-17	18&lt;/PRE&gt;&lt;P&gt;Please help...&lt;/P&gt;</description>
      <pubDate>Wed, 15 May 2019 14:37:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/change-time-series-data-into-range/m-p/558968#M156045</guid>
      <dc:creator>Srigyan</dc:creator>
      <dc:date>2019-05-15T14:37:59Z</dc:date>
    </item>
    <item>
      <title>Re: change time series data into range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/change-time-series-data-into-range/m-p/558974#M156048</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/200312"&gt;@Srigyan&lt;/a&gt;&amp;nbsp; &amp;nbsp;Can you explain the logic of this result&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;A	08-Jan-18	08-Jan-18	11
A	10-Jan-18	10-Jan-18	11&lt;/PRE&gt;
&lt;P&gt;I am unable to comprehend as I would think the price of 11 lasted from 8th to 10th?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;OH well, I see , so you are basically taking only continuous data?&lt;/P&gt;</description>
      <pubDate>Wed, 15 May 2019 14:47:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/change-time-series-data-into-range/m-p/558974#M156048</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-05-15T14:47:33Z</dc:date>
    </item>
    <item>
      <title>Re: change time series data into range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/change-time-series-data-into-range/m-p/558977#M156050</link>
      <description>Yes</description>
      <pubDate>Wed, 15 May 2019 14:48:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/change-time-series-data-into-range/m-p/558977#M156050</guid>
      <dc:creator>Srigyan</dc:creator>
      <dc:date>2019-05-15T14:48:44Z</dc:date>
    </item>
    <item>
      <title>Re: change time series data into range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/change-time-series-data-into-range/m-p/559000#M156059</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/200312"&gt;@Srigyan&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data Table1;

input product $ date date9.  price;
format date date9.;
datalines;
A 01-Jan-18 10
A 02-Jan-18 10
A 03-Jan-18 10
A 04-Jan-18 10
A 05-Jan-18 10
A 06-Jan-18 10
A 07-Jan-18 3
A 08-Jan-18 11
A 10-Jan-18 11
B 15-Jan-17 15
B 16-Jan-17 15
B 17-Jan-17 15
B 20-Jan-17 15
B 21-Jan-17 19
B 22-Jan-17 19
B 23-Jan-17 19
B 24-Jan-17 18
;

run;




data temp;
set table1;
by product price notsorted;
retain grp;
k=dif(date);
if first.product then grp=1;
else if k ne 1 or first.price then grp+1;
drop k;
run;

proc sql;
create table want as
select distinct product,min(date) as  Start_date	format=date9.,max(date) as End_date format=date9., price
from temp
group by product, grp
order by product, start_date;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Please note,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your table B 15,16, should have the year 17 and not 18 to meet your output.&lt;/P&gt;</description>
      <pubDate>Wed, 15 May 2019 15:11:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/change-time-series-data-into-range/m-p/559000#M156059</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-05-15T15:11:57Z</dc:date>
    </item>
    <item>
      <title>Re: change time series data into range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/change-time-series-data-into-range/m-p/559002#M156060</link>
      <description>&lt;PRE&gt;proc sort data=table1; by product date;
run;

data want;
set table1;
by product date price notsorted;
retain start_date;

if first.price then start_date=date;

if last.price then do;
    end_date = date; 
    output;
end;

drop date;
run;

&lt;/PRE&gt;
&lt;P&gt;Use the NOTSORTED option to identify changes in prices.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Use the RETAIN to hold the start_date the same until it ends.&lt;/P&gt;
&lt;P&gt;Use OUTPUT to explicitly output at the end of a series.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/200312"&gt;@Srigyan&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I have a following time series data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;Product	DATE	price
A	01-Jan-18	10
A	02-Jan-18	10
A	03-Jan-18	10
A	04-Jan-18	10
A	05-Jan-18	10
A	06-Jan-18	10
A	07-Jan-18	3
A	08-Jan-18	11
A	10-Jan-18	11
B	15-Jan-18	15
B	16-Jan-17	15
B	17-Jan-17	15
B	20-Jan-17	15
B	21-Jan-17	19
B	22-Jan-17	19
B	23-Jan-17	19
B	24-Jan-17	18&lt;/PRE&gt;
&lt;P&gt;code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Table1;

input product $ date date9.  price;
format date date9.;
datalines;
A 01-Jan-18 10
A 02-Jan-18 10
A 03-Jan-18 10
A 04-Jan-18 10
A 05-Jan-18 10
A 06-Jan-18 10
A 07-Jan-18 3
A 08-Jan-18 11
A 10-Jan-18 11
B 15-Jan-18 15
B 16-Jan-17 15
B 17-Jan-17 15
B 20-Jan-17 15
B 21-Jan-17 19
B 22-Jan-17 19
B 23-Jan-17 19
B 24-Jan-17 18
;

run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I wanted to have this data converted in to range.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;Product	Start_date	End_date	Price
A	01-Jan-18	06-Jan-18	10
A	07-Jan-18	07-Jan-18	3
A	08-Jan-18	08-Jan-18	11
A	10-Jan-18	10-Jan-18	11
B	15-Jan-18	17-Jan-17	15
B	20-Jan-17	20-Jan-17	15
B	21-Jan-17	23-Jan-17	19
B	24-Jan-17	24-Jan-17	18&lt;/PRE&gt;
&lt;P&gt;Please help...&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 15 May 2019 15:14:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/change-time-series-data-into-range/m-p/559002#M156060</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-05-15T15:14:07Z</dc:date>
    </item>
    <item>
      <title>Re: change time series data into range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/change-time-series-data-into-range/m-p/560177#M156569</link>
      <description>&lt;P&gt;didn't work for me.output is coming like this. date is fine but this is not giving required output.&lt;/P&gt;&lt;PRE&gt;product	price	start_date	end_date
A	10	21185	21185
A	10	21186	21186
A	10	21187	21187
A	10	21188	21188
A	10	21189	21189
A	10	21190	21190
A	3	21191	21191
A	11	21192	21192
A	11	21194	21194
B	15	20835	20835
B	15	20836	20836
B	15	20839	20839
B	19	20840	20840
B	19	20841	20841
B	19	20842	20842
B	18	20843	20843
B	15	21199	21199&lt;/PRE&gt;</description>
      <pubDate>Mon, 20 May 2019 15:14:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/change-time-series-data-into-range/m-p/560177#M156569</guid>
      <dc:creator>Srigyan</dc:creator>
      <dc:date>2019-05-20T15:14:24Z</dc:date>
    </item>
    <item>
      <title>Re: change time series data into range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/change-time-series-data-into-range/m-p/560190#M156578</link>
      <description>&lt;P&gt;thanks&lt;/P&gt;</description>
      <pubDate>Mon, 20 May 2019 15:30:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/change-time-series-data-into-range/m-p/560190#M156578</guid>
      <dc:creator>Srigyan</dc:creator>
      <dc:date>2019-05-20T15:30:50Z</dc:date>
    </item>
    <item>
      <title>Re: change time series data into range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/change-time-series-data-into-range/m-p/560200#M156583</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/200312"&gt;@Srigyan&lt;/a&gt;&amp;nbsp; &amp;nbsp;Very welcome. That was a good question. Fun stuff! &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 20 May 2019 15:43:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/change-time-series-data-into-range/m-p/560200#M156583</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-05-20T15:43:21Z</dc:date>
    </item>
  </channel>
</rss>

