<?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 Create new variable based on first occurrence in data set in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Create-new-variable-based-on-first-occurrence-in-data-set/m-p/445507#M111643</link>
    <description>New to SAS and this site&lt;BR /&gt;&lt;BR /&gt;I have a data set that I need code to help pull back first relationships manager based on the account number.&lt;BR /&gt;&lt;BR /&gt;Example data set:&lt;BR /&gt;Date ACCOUNT RM&lt;BR /&gt;1-1-18 123 Gabe&lt;BR /&gt;1-2-18. 123 Gabe&lt;BR /&gt;1-3-18. 123. Mary&lt;BR /&gt;&lt;BR /&gt;New variable would pull back ‘Gabe’ in each row.&lt;BR /&gt;&lt;BR /&gt;Data set is large and will account for each day of the year and 1000’s of accounts.&lt;BR /&gt;&lt;BR /&gt;Thanks in advance&lt;BR /&gt;</description>
    <pubDate>Wed, 14 Mar 2018 15:24:47 GMT</pubDate>
    <dc:creator>ColonelSanders</dc:creator>
    <dc:date>2018-03-14T15:24:47Z</dc:date>
    <item>
      <title>Create new variable based on first occurrence in data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-new-variable-based-on-first-occurrence-in-data-set/m-p/445507#M111643</link>
      <description>New to SAS and this site&lt;BR /&gt;&lt;BR /&gt;I have a data set that I need code to help pull back first relationships manager based on the account number.&lt;BR /&gt;&lt;BR /&gt;Example data set:&lt;BR /&gt;Date ACCOUNT RM&lt;BR /&gt;1-1-18 123 Gabe&lt;BR /&gt;1-2-18. 123 Gabe&lt;BR /&gt;1-3-18. 123. Mary&lt;BR /&gt;&lt;BR /&gt;New variable would pull back ‘Gabe’ in each row.&lt;BR /&gt;&lt;BR /&gt;Data set is large and will account for each day of the year and 1000’s of accounts.&lt;BR /&gt;&lt;BR /&gt;Thanks in advance&lt;BR /&gt;</description>
      <pubDate>Wed, 14 Mar 2018 15:24:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-new-variable-based-on-first-occurrence-in-data-set/m-p/445507#M111643</guid>
      <dc:creator>ColonelSanders</dc:creator>
      <dc:date>2018-03-14T15:24:47Z</dc:date>
    </item>
    <item>
      <title>Re: Create new variable based on first occurrence in data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-new-variable-based-on-first-occurrence-in-data-set/m-p/445510#M111644</link>
      <description>&lt;P&gt;What does 'pull back' mean?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What are you looking for as output?&lt;/P&gt;
&lt;P&gt;If you have multiple accounts it would be helpful if you included more than one in your example.&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/199071"&gt;@ColonelSanders&lt;/a&gt; wrote:&lt;BR /&gt;New to SAS and this site&lt;BR /&gt;&lt;BR /&gt;I have a data set that I need code to help pull back first relationships manager based on the account number.&lt;BR /&gt;&lt;BR /&gt;Example data set:&lt;BR /&gt;Date ACCOUNT RM&lt;BR /&gt;1-1-18 123 Gabe&lt;BR /&gt;1-2-18. 123 Gabe&lt;BR /&gt;1-3-18. 123. Mary&lt;BR /&gt;&lt;BR /&gt;New variable would pull back ‘Gabe’ in each row.&lt;BR /&gt;&lt;BR /&gt;Data set is large and will account for each day of the year and 1000’s of accounts.&lt;BR /&gt;&lt;BR /&gt;Thanks in advance&lt;BR /&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 14 Mar 2018 15:28:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-new-variable-based-on-first-occurrence-in-data-set/m-p/445510#M111644</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-03-14T15:28:23Z</dc:date>
    </item>
    <item>
      <title>Re: Create new variable based on first occurrence in data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-new-variable-based-on-first-occurrence-in-data-set/m-p/445518#M111647</link>
      <description>Wow that was fast and Sorry having to use my cell as site is blocked on work pc so made the ask to brief.&lt;BR /&gt;&lt;BR /&gt;In the end I would like the following result. Keeping the data set as is with a new variable or ‘Original RM’ being added.&lt;BR /&gt;&lt;BR /&gt;Date ACCOUNT RM Original RM&lt;BR /&gt;1-1-18 123 Gabe Gabe&lt;BR /&gt;1-2-18. 123 Gabe Gabe&lt;BR /&gt;1-3-18 123 Mary Gabe&lt;BR /&gt;1-1-18 234 Bob Bob&lt;BR /&gt;1-2-18 234 Bob Bob&lt;BR /&gt;1-3-18 234 Bob Bob&lt;BR /&gt;1-1-18 789 Sam Sam&lt;BR /&gt;1-2-18 789 Mike Sam&lt;BR /&gt;1-3-18 789 Betty Sam&lt;BR /&gt;&lt;BR /&gt;I would be using this report to capture who added the account originaly not who manages the acccount today.&lt;BR /&gt;</description>
      <pubDate>Wed, 14 Mar 2018 15:49:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-new-variable-based-on-first-occurrence-in-data-set/m-p/445518#M111647</guid>
      <dc:creator>ColonelSanders</dc:creator>
      <dc:date>2018-03-14T15:49:50Z</dc:date>
    </item>
    <item>
      <title>Re: Create new variable based on first occurrence in data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-new-variable-based-on-first-occurrence-in-data-set/m-p/445519#M111648</link>
      <description>&lt;P&gt;Is the data already sorted by ACCOUNT/DATE?&amp;nbsp; If so, then:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have (drop=rm);
  by account;
  if first.account then set have (keep=rm) point=_n_;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;The first SET statement reads each incoming record from HAVE, but does not read in variable RM.&amp;nbsp; As a result RM IS NOT REPLACED by that SET statement.&lt;/LI&gt;
&lt;LI&gt;The BY ACCT statement tells sas (a) to expect the incoming data to be sorted, and (b) create a dummy variable (first.account and last.account) indicating whether the record-in-hand is the start or end of a BY-group.&lt;/LI&gt;
&lt;LI&gt;The &lt;EM&gt;&lt;STRONG&gt;IF first.account then set have&lt;/STRONG&gt;&lt;/EM&gt; reads in only one variable (RM) and does not read sequentially, but points to observation _N_ of HAVE.&amp;nbsp; And _N_ is an automatic variable that counts iterations of the DATA step, which in this case is also a count of observations in HAVE.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Take home message: Make the program-data-vector (better known as PDV) your friend.&amp;nbsp; PDV is a list of variables in the data step.&amp;nbsp; All but one of those variables are replaced with the first set.&amp;nbsp; And the other one (RM) is replaced only occasionally.&lt;/P&gt;</description>
      <pubDate>Wed, 14 Mar 2018 15:52:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-new-variable-based-on-first-occurrence-in-data-set/m-p/445519#M111648</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2018-03-14T15:52:34Z</dc:date>
    </item>
    <item>
      <title>Re: Create new variable based on first occurrence in data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-new-variable-based-on-first-occurrence-in-data-set/m-p/445521#M111649</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
by account date;

retain original_RM;

if first.account then original_RM = RM;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You need a couple of different options here:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1. BY group processing to identify the FIRST. of each account (first.account)&lt;/P&gt;
&lt;P&gt;2. RETAIN to keep the value until it's reset.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;3. I included date in the BY to ensure the first date is used, but this also means you need to sort the data first.&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/199071"&gt;@ColonelSanders&lt;/a&gt; wrote:&lt;BR /&gt;Wow that was fast and Sorry having to use my cell as site is blocked on work pc so made the ask to brief.&lt;BR /&gt;&lt;BR /&gt;In the end I would like the following result. Keeping the data set as is with a new variable or ‘Original RM’ being added.&lt;BR /&gt;&lt;BR /&gt;Date ACCOUNT RM Original RM&lt;BR /&gt;1-1-18 123 Gabe Gabe&lt;BR /&gt;1-2-18. 123 Gabe Gabe&lt;BR /&gt;1-3-18 123 Mary Gabe&lt;BR /&gt;1-1-18 234 Bob Bob&lt;BR /&gt;1-2-18 234 Bob Bob&lt;BR /&gt;1-3-18 234 Bob Bob&lt;BR /&gt;1-1-18 789 Sam Sam&lt;BR /&gt;1-2-18 789 Mike Sam&lt;BR /&gt;1-3-18 789 Betty Sam&lt;BR /&gt;&lt;BR /&gt;I would be using this report to capture who added the account originaly not who manages the acccount today.&lt;BR /&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;BR /&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 14 Mar 2018 15:54:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-new-variable-based-on-first-occurrence-in-data-set/m-p/445521#M111649</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-03-14T15:54:15Z</dc:date>
    </item>
    <item>
      <title>Re: Create new variable based on first occurrence in data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-new-variable-based-on-first-occurrence-in-data-set/m-p/445522#M111650</link>
      <description>&lt;P&gt;Something like:&lt;/P&gt;
&lt;PRE&gt;proc sort data=have out=first;
  by account date;
run;
data first (keep=account rm_first);
  set first (rename=(rm_original=rm_first));
  by account;
  if first.account;
run;
data want;
  merge have first;
  by account;
run;&lt;/PRE&gt;
&lt;P&gt;Note, post test data in the form of a datastep in future, I am not here to type out test data, hence the above is not tested.&lt;/P&gt;</description>
      <pubDate>Wed, 14 Mar 2018 15:55:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-new-variable-based-on-first-occurrence-in-data-set/m-p/445522#M111650</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-03-14T15:55:47Z</dc:date>
    </item>
  </channel>
</rss>

