<?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: Help summarizing attributes in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Help-summarizing-attributes/m-p/37119#M7340</link>
    <description>Patrick,&lt;BR /&gt;
&lt;BR /&gt;
Thank you for your reply.  I will get right to work applying the flags.  As I am sure you guessed the actual data set is more complex but I tried to make my example easy.  The actual data has a quality like "buy beer" but it is a one time event unlike actual grocery purchases where one could buy beer on multiple dates.&lt;BR /&gt;
&lt;BR /&gt;
Thank you again,&lt;BR /&gt;
Melissa</description>
    <pubDate>Wed, 06 Jan 2010 12:21:48 GMT</pubDate>
    <dc:creator>deleted_user</dc:creator>
    <dc:date>2010-01-06T12:21:48Z</dc:date>
    <item>
      <title>Help summarizing attributes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-summarizing-attributes/m-p/37117#M7338</link>
      <description>Hello, I have a data set with two sorts of “attributes” (probably not using term in strict database sense).&lt;BR /&gt;
&lt;BR /&gt;
I have a perminant attribute of the unique identifier (for example gender).  I have multiple records per identifier which describe a transaction (like an item purchased).&lt;BR /&gt;
&lt;BR /&gt;
“ID” – unique identifier&lt;BR /&gt;
“Sex” – gender of ID person&lt;BR /&gt;
“Product” – what was purchased (beer, diapers, pencils)&lt;BR /&gt;
Purch_dt – date of purchase&lt;BR /&gt;
&lt;BR /&gt;
1234	F	beer		Nov 1, 2008&lt;BR /&gt;
1234	F	diapers		Nov 24, 2008&lt;BR /&gt;
5678	M	beer		Nov 17, 2008&lt;BR /&gt;
5678	M	pencils		Nov 30, 2008&lt;BR /&gt;
&lt;BR /&gt;
I want to &lt;BR /&gt;
- count the number of people who purchased both beer and diapers&lt;BR /&gt;
- count the number of people who purchased beer and no diapers&lt;BR /&gt;
- determine the number of women who purchased beer and diapers&lt;BR /&gt;
- for those who purchased both, determine the time lag between the purchase of beer and diapers&lt;BR /&gt;
&lt;BR /&gt;
I have not written successful code but had two different thoughts (but am willing to do anything that will work).  &lt;BR /&gt;
&lt;BR /&gt;
I tried proc SQL “group by ID” but my instinct in English would to say … by ID where Product = beer and Product = diapers but I do not believe this is correct.&lt;BR /&gt;
&lt;BR /&gt;
I tried a do loop by ID and flag if Product = beer and if Product = diapers then do, by unique IDs count flags but this was cumbersome and while the code did something I am not sure it was correct.</description>
      <pubDate>Tue, 05 Jan 2010 19:59:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-summarizing-attributes/m-p/37117#M7338</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2010-01-05T19:59:31Z</dc:date>
    </item>
    <item>
      <title>Re: Help summarizing attributes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-summarizing-attributes/m-p/37118#M7339</link>
      <description>"for those who purchased both, determine the time lag between the purchase of beer and diapers"&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
What would be the time lag for the following case? Or would there be several time lags?&lt;BR /&gt;
1234 F beer Nov 1, 2008&lt;BR /&gt;
1234 F diapers Nov 24, 2008&lt;BR /&gt;
1234 M beer Nov 17, 2008&lt;BR /&gt;
1234 M pencils Nov 30, 2008&lt;BR /&gt;
1234 F diapers Dec 1, 2008&lt;BR /&gt;
1234 F beer Dec 24, 2008&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
For the other 3 requirements:&lt;BR /&gt;
&lt;BR /&gt;
data have;&lt;BR /&gt;
  infile datalines truncover;&lt;BR /&gt;
  input id sex:$1. product:$8. purch_dt anydtdte12.;&lt;BR /&gt;
  format purch_dt date9.;&lt;BR /&gt;
  datalines;&lt;BR /&gt;
1234 F beer Nov 1, 2008&lt;BR /&gt;
1234 F diapers Nov 24, 2008&lt;BR /&gt;
5678 M beer Nov 17, 2008&lt;BR /&gt;
5678 M pencils Nov 30, 2008&lt;BR /&gt;
;&lt;BR /&gt;
&lt;BR /&gt;
data want;&lt;BR /&gt;
  set have;&lt;BR /&gt;
  by id;&lt;BR /&gt;
  retain flag '00';&lt;BR /&gt;
  select (product);&lt;BR /&gt;
    when ('beer') substr(flag,1,1)='1';&lt;BR /&gt;
    when ('diapers') substr(flag,2,1)='1';&lt;BR /&gt;
    otherwise;&lt;BR /&gt;
  end;&lt;BR /&gt;
  if last.id then&lt;BR /&gt;
  do;&lt;BR /&gt;
    output;&lt;BR /&gt;
    flag='00';&lt;BR /&gt;
  end;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
proc format;&lt;BR /&gt;
  value $flag&lt;BR /&gt;
    '00' = 'No Beer / No Diapers'&lt;BR /&gt;
    '10' = 'Beer / No Diapers'&lt;BR /&gt;
    '01' = 'No Beer / Diapers'&lt;BR /&gt;
    '11' = 'Beer / Diapers'&lt;BR /&gt;
    ;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
proc tabulate data=want;&lt;BR /&gt;
  class sex flag;&lt;BR /&gt;
  format flag $flag.;&lt;BR /&gt;
  keylabel all='Total' n=' ';&lt;BR /&gt;
  table sex='Gender' all,(flag='Products Purchased' all)*f=best12.&lt;BR /&gt;
  ;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
HTH&lt;BR /&gt;
Patrick

Message was edited by: Patrick</description>
      <pubDate>Wed, 06 Jan 2010 01:28:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-summarizing-attributes/m-p/37118#M7339</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2010-01-06T01:28:24Z</dc:date>
    </item>
    <item>
      <title>Re: Help summarizing attributes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-summarizing-attributes/m-p/37119#M7340</link>
      <description>Patrick,&lt;BR /&gt;
&lt;BR /&gt;
Thank you for your reply.  I will get right to work applying the flags.  As I am sure you guessed the actual data set is more complex but I tried to make my example easy.  The actual data has a quality like "buy beer" but it is a one time event unlike actual grocery purchases where one could buy beer on multiple dates.&lt;BR /&gt;
&lt;BR /&gt;
Thank you again,&lt;BR /&gt;
Melissa</description>
      <pubDate>Wed, 06 Jan 2010 12:21:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-summarizing-attributes/m-p/37119#M7340</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2010-01-06T12:21:48Z</dc:date>
    </item>
    <item>
      <title>Re: Help summarizing attributes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-summarizing-attributes/m-p/37120#M7341</link>
      <description>Hi Melissa&lt;BR /&gt;
&lt;BR /&gt;
The way you describe your data the following approach might also be interesting for you:&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
data have;&lt;BR /&gt;
  infile datalines truncover;&lt;BR /&gt;
  input id sex:$1. product:$8. purch_dt anydtdte12.;&lt;BR /&gt;
  format purch_dt date9.;&lt;BR /&gt;
  datalines;&lt;BR /&gt;
1234 F beer Nov 1, 2008&lt;BR /&gt;
1234 F diapers Nov 24, 2008&lt;BR /&gt;
5678 M beer Nov 17, 2008&lt;BR /&gt;
5678 M pencils Nov 30, 2008&lt;BR /&gt;
;&lt;BR /&gt;
&lt;BR /&gt;
proc sql;&lt;BR /&gt;
  create view Vhave as&lt;BR /&gt;
    select * from have &lt;BR /&gt;
      where product in ('beer','diapers')&lt;BR /&gt;
    order by id,sex;&lt;BR /&gt;
quit;&lt;BR /&gt;
&lt;BR /&gt;
proc transpose data=Vhave out=want;&lt;BR /&gt;
  by id sex;&lt;BR /&gt;
  id product;&lt;BR /&gt;
  var purch_dt;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
data want;&lt;BR /&gt;
  set want;&lt;BR /&gt;
  TimeLagInDays=datdif(beer, diapers, 'act/act');&lt;BR /&gt;
  BeerAndDiapers= not missing(beer+diapers);&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
options missing=' ';&lt;BR /&gt;
proc tabulate data=want;&lt;BR /&gt;
  class sex;&lt;BR /&gt;
  var beer diapers BeerAndDiapers TimeLagInDays;&lt;BR /&gt;
  keylabel all='Total' n=' ' sum=' ';&lt;BR /&gt;
  table sex='Gender' all, (beer*n diapers*n BeerAndDiapers*sum TimeLagInDays*mean)*f=best12.&lt;BR /&gt;
  ;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
HTH&lt;BR /&gt;
Patrick</description>
      <pubDate>Thu, 07 Jan 2010 09:01:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-summarizing-attributes/m-p/37120#M7341</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2010-01-07T09:01:19Z</dc:date>
    </item>
    <item>
      <title>Re: Help summarizing attributes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-summarizing-attributes/m-p/37121#M7342</link>
      <description>Melissa&lt;BR /&gt;
another approach&lt;BR /&gt;
when you want to bring together different kinds of events &lt;BR /&gt;
merge the different types like[pre] data combo ;&lt;BR /&gt;
   merge whole_Lot( in=beers where=( criteria for beer event) )&lt;BR /&gt;
         whole_Lot( in=diapr where=( criteria for diapers ) ) ;&lt;BR /&gt;
       by ID ;&lt;BR /&gt;
   join = put( 10* beers + diapr, z2. ) ;&lt;BR /&gt;
   if first.ID ;&lt;BR /&gt;
run ; [/pre]&lt;BR /&gt;
This simple approach asumes your data are in ID order&lt;BR /&gt;
The JOIN variable takes 3 values&lt;BR /&gt;
11 both diapers and beers&lt;BR /&gt;
10 beers only&lt;BR /&gt;
01 diapers only&lt;BR /&gt;
You might want to take out the "if first.ID; " statement to see all transactions through the merge ( but then use a test like "if _n_ &amp;gt; 1000 then stop; ".&lt;BR /&gt;
If you need to examine more criteria, like the time between transactions, do as much as possible on the where clauses applied to the data of the merge.&lt;BR /&gt;
 .&lt;BR /&gt;
Good Luck&lt;BR /&gt;
 &lt;BR /&gt;
PeterC</description>
      <pubDate>Thu, 07 Jan 2010 19:35:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-summarizing-attributes/m-p/37121#M7342</guid>
      <dc:creator>Peter_C</dc:creator>
      <dc:date>2010-01-07T19:35:22Z</dc:date>
    </item>
  </channel>
</rss>

