<?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: Trying to preprocess the sas dataset in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Trying-to-preprocess-the-sas-dataset/m-p/451415#M283819</link>
    <description>&lt;P&gt;Be cautious. Just grouping by id and job_cd will not solve the fact that id 1 has two separate periods with job_cd = 4.&lt;/P&gt;
&lt;P&gt;I propose some trickery:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data
  int1 (keep=id job_cd period year)
  int2 (keep=id period first_year last_year)
;
set work;
by id job_cd notsorted;
retain period_count first_year;
if first.id then period = 0;
if first.job_cd
then do;
  period + 1;
  first_year = year;
end;
if last.job_cd
then do;
  last_year = year;
  output int2;
end;
output int1;
run;

data want;
merge
  int1
  int2
;
by id period;
drop period;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 05 Apr 2018 07:06:34 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2018-04-05T07:06:34Z</dc:date>
    <item>
      <title>Trying to preprocess the sas dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Trying-to-preprocess-the-sas-dataset/m-p/451364#M283817</link>
      <description>&lt;P&gt;Hi, I am newbie to SAS and i need help desperately.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;getting straight to the point, I am trying to process the sas table into another table as described below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Given:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;data work;&lt;BR /&gt;input ID JOB_CD YEAR;&lt;BR /&gt;cards;&lt;BR /&gt;1 4 2006&lt;BR /&gt;1 4 2007&lt;BR /&gt;1 3 2008&lt;BR /&gt;1 5 2009&lt;BR /&gt;1 4 2010&lt;BR /&gt;2 5 2005&lt;BR /&gt;2 5 2006&lt;BR /&gt;7 1 2007&lt;/P&gt;&lt;P&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;WANT:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID&amp;nbsp; &amp;nbsp; &amp;nbsp;JOB_CD&amp;nbsp; &amp;nbsp; YEAR&amp;nbsp; &amp;nbsp;FIRST_YEAR&amp;nbsp; &amp;nbsp; LAST_YEAR&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2006&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2006&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2007&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2007&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2006&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2007&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2008&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2008&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2008&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2009&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2009&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2009&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2010&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2010&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2010&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2005&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2005&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2006&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2006&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2005&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2006&lt;/P&gt;&lt;P&gt;7&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2007&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2007&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2007&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;the actual data is much larger but,&lt;/P&gt;&lt;P&gt;basically I am trying to group the data by ID and JOB_CD, and get first_year and last_year for each job within each ID.&lt;/P&gt;&lt;P&gt;It seemed to work with using "BY ID JOB_CD", FIRST.ID, LAST.ID, RETAIN statements but the problem is, given the case above,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;JOB_CD&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;YEAR&amp;nbsp; &amp;nbsp; &amp;nbsp; FIRST_YEAR&amp;nbsp; &amp;nbsp; &amp;nbsp;LAST_YEAR&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2006&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2006&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2010&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;but as you can see this is not what i want. This guy with ID 1 had had a job 4 then changed a job for a few years before changing back to the original job.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It does not matter if I use SQL for it.&lt;/P&gt;&lt;P&gt;Simpler the better, but I really want this to work.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for reading.&lt;/P&gt;</description>
      <pubDate>Thu, 05 Apr 2018 01:25:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Trying-to-preprocess-the-sas-dataset/m-p/451364#M283817</guid>
      <dc:creator>nor5teo</dc:creator>
      <dc:date>2018-04-05T01:25:19Z</dc:date>
    </item>
    <item>
      <title>Re: Trying to preprocess the sas dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Trying-to-preprocess-the-sas-dataset/m-p/451388#M283818</link>
      <description>&lt;P&gt;While there are a couple of ways to do this, here are some tools that I expect would be "next in line" for you to learn.&amp;nbsp; You can skip the PROC SORT if the data set is already in the proper order.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sort data=have;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;by id job_cd;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;proc summary data=have;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;var year;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;by id job_cd;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;output out=years (drop=_type_ _freq_) min(year) = first_year max(year) = last_year;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;merge have years;&lt;/P&gt;
&lt;P&gt;by id job_cd;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;</description>
      <pubDate>Thu, 05 Apr 2018 03:35:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Trying-to-preprocess-the-sas-dataset/m-p/451388#M283818</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-04-05T03:35:53Z</dc:date>
    </item>
    <item>
      <title>Re: Trying to preprocess the sas dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Trying-to-preprocess-the-sas-dataset/m-p/451415#M283819</link>
      <description>&lt;P&gt;Be cautious. Just grouping by id and job_cd will not solve the fact that id 1 has two separate periods with job_cd = 4.&lt;/P&gt;
&lt;P&gt;I propose some trickery:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data
  int1 (keep=id job_cd period year)
  int2 (keep=id period first_year last_year)
;
set work;
by id job_cd notsorted;
retain period_count first_year;
if first.id then period = 0;
if first.job_cd
then do;
  period + 1;
  first_year = year;
end;
if last.job_cd
then do;
  last_year = year;
  output int2;
end;
output int1;
run;

data want;
merge
  int1
  int2
;
by id period;
drop period;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 05 Apr 2018 07:06:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Trying-to-preprocess-the-sas-dataset/m-p/451415#M283819</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-04-05T07:06:34Z</dc:date>
    </item>
    <item>
      <title>Re: Trying to preprocess the sas dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Trying-to-preprocess-the-sas-dataset/m-p/451423#M283820</link>
      <description>while it's useful. it is not quite i was looking for. But thanks anyway.</description>
      <pubDate>Thu, 05 Apr 2018 08:38:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Trying-to-preprocess-the-sas-dataset/m-p/451423#M283820</guid>
      <dc:creator>nor5teo</dc:creator>
      <dc:date>2018-04-05T08:38:02Z</dc:date>
    </item>
    <item>
      <title>Re: Trying to preprocess the sas dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Trying-to-preprocess-the-sas-dataset/m-p/451424#M283821</link>
      <description>Thanks for saving my day. you are the best.</description>
      <pubDate>Thu, 05 Apr 2018 08:39:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Trying-to-preprocess-the-sas-dataset/m-p/451424#M283821</guid>
      <dc:creator>nor5teo</dc:creator>
      <dc:date>2018-04-05T08:39:54Z</dc:date>
    </item>
    <item>
      <title>Re: Trying to preprocess the sas dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Trying-to-preprocess-the-sas-dataset/m-p/451425#M283822</link>
      <description>&lt;P&gt;You can use &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954"&gt;@Astounding&lt;/a&gt;'s method by first creating a period variable like I did and then use that in the proc summary and the merge instead of job_cd.&lt;/P&gt;</description>
      <pubDate>Thu, 05 Apr 2018 08:41:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Trying-to-preprocess-the-sas-dataset/m-p/451425#M283822</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-04-05T08:41:57Z</dc:date>
    </item>
  </channel>
</rss>

