<?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 sql and group to separate out instances chronologically in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-and-group-to-separate-out-instances-chronologically/m-p/267135#M52753</link>
    <description>&lt;P&gt;Consider the following SAS code:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
format date date9.
ctry_cd $2.
sn $2.;
input ctry_cd sn date;
datalines;
US 1 20000
US 1 20001
US 1 20002
CA 1 20003
CA 1 20004
US 1 20005
US 1 20006
US 1 20007
ES 2 20001
ES 2 20002
;
run;

proc sql;
create table check as
select
sn,
ctry_cd,
min(date) as begin_dt,
max(date) as end_dt
from test
group by sn, ctry_cd;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;This returns:&lt;/P&gt;&lt;PRE&gt;1 CA 07OCT2014 08OCT2014
1 US 04OCT2014 11OCT2014
2 ES 05OCT2014 06OCT2014&lt;/PRE&gt;&lt;P&gt;I would like for the `proc sql` distinguish between the country moves; that is, return&lt;/P&gt;&lt;PRE&gt;1 US 04OCT2014 06OCT2014
1 CA 07OCT2014 08OCT2014
1 US 09OCT2014 11OCT2014
2 ES 05OCT2014 06OCT2014&lt;/PRE&gt;&lt;P&gt;So it still groups the instances by sn and ctry_nm but pays attention to the date so I have a timeline.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 28 Apr 2016 22:58:58 GMT</pubDate>
    <dc:creator>dwsmith</dc:creator>
    <dc:date>2016-04-28T22:58:58Z</dc:date>
    <item>
      <title>proc sql and group to separate out instances chronologically</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-and-group-to-separate-out-instances-chronologically/m-p/267135#M52753</link>
      <description>&lt;P&gt;Consider the following SAS code:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
format date date9.
ctry_cd $2.
sn $2.;
input ctry_cd sn date;
datalines;
US 1 20000
US 1 20001
US 1 20002
CA 1 20003
CA 1 20004
US 1 20005
US 1 20006
US 1 20007
ES 2 20001
ES 2 20002
;
run;

proc sql;
create table check as
select
sn,
ctry_cd,
min(date) as begin_dt,
max(date) as end_dt
from test
group by sn, ctry_cd;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;This returns:&lt;/P&gt;&lt;PRE&gt;1 CA 07OCT2014 08OCT2014
1 US 04OCT2014 11OCT2014
2 ES 05OCT2014 06OCT2014&lt;/PRE&gt;&lt;P&gt;I would like for the `proc sql` distinguish between the country moves; that is, return&lt;/P&gt;&lt;PRE&gt;1 US 04OCT2014 06OCT2014
1 CA 07OCT2014 08OCT2014
1 US 09OCT2014 11OCT2014
2 ES 05OCT2014 06OCT2014&lt;/PRE&gt;&lt;P&gt;So it still groups the instances by sn and ctry_nm but pays attention to the date so I have a timeline.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 28 Apr 2016 22:58:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-and-group-to-separate-out-instances-chronologically/m-p/267135#M52753</guid>
      <dc:creator>dwsmith</dc:creator>
      <dc:date>2016-04-28T22:58:58Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql and group to separate out instances chronologically</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-and-group-to-separate-out-instances-chronologically/m-p/267143#M52754</link>
      <description>&lt;P&gt;Proc means to the rescue!&lt;/P&gt;
&lt;P&gt;The NOTSORTED option will allow it to process the data in the order it encounters it, so this does assume your data is in the order you want it.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc means data=test min max;
by sn ctry_cd notsorted;
var date;
ods output summary=want;
run;

proc print data=want;
format date: date9.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Another option would be to create a group identifier for each group within the dataset, using a data step and then apply a proc sql step.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 28 Apr 2016 23:42:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-and-group-to-separate-out-instances-chronologically/m-p/267143#M52754</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-04-28T23:42:08Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql and group to separate out instances chronologically</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-and-group-to-separate-out-instances-chronologically/m-p/267157#M52759</link>
      <description>&lt;P&gt;You need a data step or a data view to deal with sequential aspect of your processing. Here is how it can be done with a data view:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
format date date9.
ctry_cd $2.
sn $2.;
input ctry_cd sn date;
datalines;
US 1 20000
US 1 20001
US 1 20002
CA 1 20003
CA 1 20004
US 1 20005
US 1 20006
US 1 20007
ES 2 20001
ES 2 20002
;

data testSeq / view=testSeq;
set test; by ctry_cd notsorted;
seq + first.ctry_cd;
run;

proc sql;
create table check as
select 
    sn, 
    ctry_cd,
    min(date) as begin_dt format=date9.,
    max(date) as end_dt format=date9.
from testSeq
group by seq, sn, ctry_cd;
drop view testSeq;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 29 Apr 2016 02:59:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-and-group-to-separate-out-instances-chronologically/m-p/267157#M52759</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2016-04-29T02:59:48Z</dc:date>
    </item>
  </channel>
</rss>

