<?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: Creating a new column based on the frequency of a variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-new-column-based-on-the-frequency-of-a-variable/m-p/333392#M75113</link>
    <description>&lt;P&gt;Thanks for your help! Your solution worked perfectly&lt;/P&gt;</description>
    <pubDate>Thu, 16 Feb 2017 14:32:37 GMT</pubDate>
    <dc:creator>hk1234</dc:creator>
    <dc:date>2017-02-16T14:32:37Z</dc:date>
    <item>
      <title>Creating a new column based on the frequency of a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-new-column-based-on-the-frequency-of-a-variable/m-p/332965#M74989</link>
      <description>&lt;DIV&gt;Hi,&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;I am using sas 9.4 base&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;I want to measure the frequency of a each variable within a column (similar to proc freq) and then create a second column which states the frequency nxt to ac instance of the variable.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;The data is from Facebook and has an individual row per action (like, comment, share) with an actor Id column (so if the actor has liked a post more than once it is on a separate row). So I want to rank each actor by the number of actions that he/she has taken.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;This can be done with a proc freq report but I'm not sure how to create a column stating how many actions each actor has taken on every row.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Further it would be awesome if the time between action (each row is an action) can be measured on the actor level.... pls see the attached screenshot&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Any help would be greatly appreciated!&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;-Harrison&lt;/DIV&gt;&lt;BR /&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/13450i1E3C61BDF0B531DA/image-size/large?v=1.0&amp;amp;px=600" border="0" alt="data screenshot.png" title="data screenshot.png" /&gt;</description>
      <pubDate>Wed, 15 Feb 2017 12:07:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-new-column-based-on-the-frequency-of-a-variable/m-p/332965#M74989</guid>
      <dc:creator>hk1234</dc:creator>
      <dc:date>2017-02-15T12:07:32Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a new column based on the frequency of a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-new-column-based-on-the-frequency-of-a-variable/m-p/332970#M74990</link>
      <description>&lt;P&gt;Use proc freq to get counts and then transpose to reshape the data to the form you want.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 15 Feb 2017 12:13:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-new-column-based-on-the-frequency-of-a-variable/m-p/332970#M74990</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-02-15T12:13:32Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a new column based on the frequency of a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-new-column-based-on-the-frequency-of-a-variable/m-p/332975#M74991</link>
      <description>&lt;P&gt;Can you post the code?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 15 Feb 2017 12:30:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-new-column-based-on-the-frequency-of-a-variable/m-p/332975#M74991</guid>
      <dc:creator>hk1234</dc:creator>
      <dc:date>2017-02-15T12:30:29Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a new column based on the frequency of a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-new-column-based-on-the-frequency-of-a-variable/m-p/332991#M74994</link>
      <description>&lt;P&gt;Your data is a picture.&amp;nbsp; You need to post data that community members interested in helping you can use in a program.&amp;nbsp; For example.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data fb;
   infile cards dsd missover;
   input (list of variable names)(informats);
   cards;
value,value,value,...
value,value,value,...
value,value,value,..
;;;;
   run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 15 Feb 2017 13:21:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-new-column-based-on-the-frequency-of-a-variable/m-p/332991#M74994</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2017-02-15T13:21:42Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a new column based on the frequency of a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-new-column-based-on-the-frequency-of-a-variable/m-p/332993#M74995</link>
      <description>&lt;P&gt;Can you post what the input data looks like in SAS?&amp;nbsp; And what you want the output data to look like?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think you want for every record, two new variables&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;actor_freq:&amp;nbsp;&amp;nbsp; The overall frequency of the actorid, taken from proc freq output&lt;/LI&gt;
&lt;LI&gt;time_lapse:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Time since last action by the same actorid&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;Absent your posting testable date ...&lt;/P&gt;
&lt;P&gt;Assuming your data are sorted by the time stamp UPDATEDDATE, then this untested code would be a useful approach:&amp;nbsp; It uses hash objects name FREQ (taken from a data set made by proc freq) and PRIOR (built dynamically during the "DATA WANT" step):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc freq data=have  noprint;
  tables actorid / out=lookupfreqs (keep=actorid count  rename=(count=actor_freq));
run;

data want;
  set have;
  if _n_=1 then do;
    if 0 then set lookupfreqs;
      declare hash freq(dataset:'lookupfreqs');
        freq.definekey('actorid');
        freq.definedata('actor_freq');
        freq.definedone();

    format prior_update datetime20.;
      declare hash prior();
        prior.definekey('actorid');
        prior.definedata('prior_update');
        prior.definedone();
  end;

  rc=freq.find();

  rc=prior.find();
  if rc=0 then time_lapse=updateddate - prior_update;
  prior_update=updateddate;
  rc=prior.replace();
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Notes:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;I repeat, dataset HAVE is assumed to be sorted by time stamp.&lt;/LI&gt;
&lt;LI&gt;You want to retrieve the frequency of&amp;nbsp;total&amp;nbsp;records by actorid,&amp;nbsp;not by actorid/action_type.&amp;nbsp; That's what the hash object FREQS holds, taken directly from proc freq output.&lt;/LI&gt;
&lt;LI&gt;UPDATEDDATE is a sas datetime value.&lt;/LI&gt;
&lt;LI&gt;You want the time_lapse since the last action of any type by the same actorid.&lt;/LI&gt;
&lt;LI&gt;The hash object&amp;nbsp;PRIOR, unlike FREQ, is&amp;nbsp;dynamically built.&amp;nbsp; When an actoirid is enountered for the first time, the statement "rc=prior.find()" will generate a non-zero rc, so no time_lapse can be calculated.&amp;nbsp;&amp;nbsp;But sebsequent find's for the same id will succeed (rc=0).&amp;nbsp; This object is meant to hold one item per actorid, always containing the time stamp of the most recent&amp;nbsp;prior action of that actorid.&amp;nbsp;&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;One could also re-sort the dataset by actorid/updateddate, and then more easily (i.e. without a hash object) get time_lapse in a data step, but I assume your data would be large enough to make a proc sort costly.&lt;/P&gt;</description>
      <pubDate>Wed, 15 Feb 2017 13:25:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-new-column-based-on-the-frequency-of-a-variable/m-p/332993#M74995</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2017-02-15T13:25:15Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a new column based on the frequency of a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-new-column-based-on-the-frequency-of-a-variable/m-p/333041#M75000</link>
      <description>&lt;P&gt;Instructions here: &lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712" target="_blank"&gt;https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712&lt;/A&gt; will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the {i} icon or attached as text to show exactly what you have and that we can test code against.&lt;/P&gt;</description>
      <pubDate>Wed, 15 Feb 2017 15:09:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-new-column-based-on-the-frequency-of-a-variable/m-p/333041#M75000</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-02-15T15:09:47Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a new column based on the frequency of a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-new-column-based-on-the-frequency-of-a-variable/m-p/333392#M75113</link>
      <description>&lt;P&gt;Thanks for your help! Your solution worked perfectly&lt;/P&gt;</description>
      <pubDate>Thu, 16 Feb 2017 14:32:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-new-column-based-on-the-frequency-of-a-variable/m-p/333392#M75113</guid>
      <dc:creator>hk1234</dc:creator>
      <dc:date>2017-02-16T14:32:37Z</dc:date>
    </item>
  </channel>
</rss>

