<?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: using SAS Macro to create complex data set in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Help-using-SAS-Macro-to-create-complex-data-set/m-p/71323#M15423</link>
    <description>Hi,&lt;BR /&gt;
&lt;BR /&gt;
Thank you very much for your reply.&lt;BR /&gt;
&lt;BR /&gt;
1) Since I will have many other different variables with the same transformation, so it would be great to have a macro to run them all. But datastep or Proc SQL would be OK .&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
(2)I tried to revised the above example to make it clear.&lt;BR /&gt;
&lt;BR /&gt;
Account	date	Time	ID	hit&lt;BR /&gt;
387000	6/10/2010	12:00:00	13.15	1&lt;BR /&gt;
387000	6/9/2010	12:00:00	13.15	1&lt;BR /&gt;
387000	6/8/2010	12:00:00	13.14	1&lt;BR /&gt;
387000	6/6/2010	11:00:00	13.15	1&lt;BR /&gt;
387000	6/5/2010	10:00:00	13.14	1&lt;BR /&gt;
387000	6/4/2010	9:00:00	13.13	0&lt;BR /&gt;
387000	6/4/2010	8:00:00	13.13	0&lt;BR /&gt;
386000	5/9/2010	10:00:00	12.12	0&lt;BR /&gt;
386000	5/2/2010	9:00:00	12.14	1&lt;BR /&gt;
386000	5/1/2010	8:00:00	12.13	0&lt;BR /&gt;
385000	3/3/2010	10:00:00	11.13	1&lt;BR /&gt;
385000	3/1/2010	8:00:00	11.12	0&lt;BR /&gt;
385000	3/1/2010	9:00:00	11.12	0&lt;BR /&gt;
&lt;BR /&gt;
For example,&lt;BR /&gt;
(1) For account 387000, since on 06/10/2010, the ID used is 13.15, it is different than the ID of 13.14 used on 06/08/2010 (which is within two days of 06/10/2010), so the hit is 1.&lt;BR /&gt;
&lt;BR /&gt;
(2) For Account 387000, since on 06/09/2010, the ID used is 13.15, it is different than the ID of 13.14 used on 06/08/2010(which is within two days of 06/09/2010), so the hit is 1.&lt;BR /&gt;
&lt;BR /&gt;
(3)For Account 387000, since on 06/04/2010 at 9:00am, the ID used is 13.13, within 2 days, the only date is another ID of same 13.13 on 06/04/2010 at time of 8:00am. The hit is 0.&lt;BR /&gt;
&lt;BR /&gt;
(4)For account 386000, within 2 days of 05/09/2010 would be 05/08/2010 or 05/07/2010. But there is no date likes these two, so the hit is 0.&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Thanks again.

Message was edited by: NIU_YA</description>
    <pubDate>Mon, 30 May 2011 23:24:16 GMT</pubDate>
    <dc:creator>NIUNIU</dc:creator>
    <dc:date>2011-05-30T23:24:16Z</dc:date>
    <item>
      <title>Help: using SAS Macro to create complex data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-using-SAS-Macro-to-create-complex-data-set/m-p/71321#M15421</link>
      <description>I have a data set similar to the following simple data set. With the four column of Account, Date, Time and ID, I like to using SAS macro to create a column like "HIT" with 0 and 1 value. The logic is " there is more than 1 of ID used within 2 days from the same account." If the logic is satisfied, then the variable HIT is 1, else is 0.&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Account    date              Time             ID    hit&lt;BR /&gt;
387000    6/10/2010    14:00:00    13.15    1&lt;BR /&gt;
387000    6/9/2010    13:00:00    13.15    0&lt;BR /&gt;
387000    6/8/2010    12:00:00    13.14    1&lt;BR /&gt;
387000    6/6/2010    11:00:00    13.15    1&lt;BR /&gt;
387000    6/5/2010    10:00:00    13.14    1&lt;BR /&gt;
387000    6/4/2010    8:00:00    13.13    0&lt;BR /&gt;
387000    6/4/2010    9:00:00    13.13    0&lt;BR /&gt;
386000    5/9/2010    10:00:00    12.12    0&lt;BR /&gt;
386000    5/2/2010    9:00:00    12.14    1&lt;BR /&gt;
386000    5/1/2010    8:00:00    12.13    0&lt;BR /&gt;
385000    3/3/2010    10:00:00    11.13    1&lt;BR /&gt;
385000    3/1/2010    8:00:00    11.12    0&lt;BR /&gt;
385000    3/1/2010    9:00:00    11.12    0&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Below is my logic trying to create this variable. But I cannot continue anymore. I would really appreciate someone can help to instruct me out of this puzzle. Thank you very much!&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
proc sort data=file; by account decending date decending time;run;&lt;BR /&gt;
&lt;BR /&gt;
%macro test;&lt;BR /&gt;
&lt;BR /&gt;
proc sql noprint;&lt;BR /&gt;
select count(distinct account)&lt;BR /&gt;
into :num_cin&lt;BR /&gt;
from file;&lt;BR /&gt;
quit;&lt;BR /&gt;
%let num_cin=&amp;amp;num_cin;&lt;BR /&gt;
%put num_cin=&amp;amp;num_cin;&lt;BR /&gt;
&lt;BR /&gt;
proc sql noprint;&lt;BR /&gt;
select distinct account&lt;BR /&gt;
into :CIN1-:CIN&amp;amp;num_cin&lt;BR /&gt;
from file;&lt;BR /&gt;
quit;&lt;BR /&gt;
%put CIN1-:CIN&amp;amp;num_cin ;&lt;BR /&gt;
&lt;BR /&gt;
**assign date **;&lt;BR /&gt;
%do i=1 %to &amp;amp;num_cin;&lt;BR /&gt;
&lt;BR /&gt;
proc sql noprint;&lt;BR /&gt;
    select count(date)&lt;BR /&gt;
        into :num_date&lt;BR /&gt;
        from file&lt;BR /&gt;
        where account=&amp;amp;&amp;amp;cin&amp;amp;i&lt;BR /&gt;
        ;&lt;BR /&gt;
quit;&lt;BR /&gt;
%let num_date=&amp;amp;num_date;&lt;BR /&gt;
%put num_date=&amp;amp;num_date &amp;amp;&amp;amp;cin&amp;amp;i;&lt;BR /&gt;
&lt;BR /&gt;
proc sql noprint;&lt;BR /&gt;
    select date&lt;BR /&gt;
        into :dat1-:dat&amp;amp;num_date&lt;BR /&gt;
        from file a&lt;BR /&gt;
        where account=&amp;amp;&amp;amp;cin&amp;amp;i&lt;BR /&gt;
        ;&lt;BR /&gt;
quit;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
%end;&lt;BR /&gt;
%mend test;&lt;BR /&gt;
%test;</description>
      <pubDate>Mon, 30 May 2011 22:18:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-using-SAS-Macro-to-create-complex-data-set/m-p/71321#M15421</guid>
      <dc:creator>NIUNIU</dc:creator>
      <dc:date>2011-05-30T22:18:15Z</dc:date>
    </item>
    <item>
      <title>Re: Help: using SAS Macro to create complex data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-using-SAS-Macro-to-create-complex-data-set/m-p/71322#M15422</link>
      <description>Questions first:&lt;BR /&gt;
1. Do you have to have a macro or will a datastep or Proc SQL work?&lt;BR /&gt;
2. How are you defining hit exactly? maybe I'm missing something but it doesn't look like 48 hours between ids in the sample data set.</description>
      <pubDate>Mon, 30 May 2011 22:59:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-using-SAS-Macro-to-create-complex-data-set/m-p/71322#M15422</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2011-05-30T22:59:04Z</dc:date>
    </item>
    <item>
      <title>Re: Help: using SAS Macro to create complex data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-using-SAS-Macro-to-create-complex-data-set/m-p/71323#M15423</link>
      <description>Hi,&lt;BR /&gt;
&lt;BR /&gt;
Thank you very much for your reply.&lt;BR /&gt;
&lt;BR /&gt;
1) Since I will have many other different variables with the same transformation, so it would be great to have a macro to run them all. But datastep or Proc SQL would be OK .&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
(2)I tried to revised the above example to make it clear.&lt;BR /&gt;
&lt;BR /&gt;
Account	date	Time	ID	hit&lt;BR /&gt;
387000	6/10/2010	12:00:00	13.15	1&lt;BR /&gt;
387000	6/9/2010	12:00:00	13.15	1&lt;BR /&gt;
387000	6/8/2010	12:00:00	13.14	1&lt;BR /&gt;
387000	6/6/2010	11:00:00	13.15	1&lt;BR /&gt;
387000	6/5/2010	10:00:00	13.14	1&lt;BR /&gt;
387000	6/4/2010	9:00:00	13.13	0&lt;BR /&gt;
387000	6/4/2010	8:00:00	13.13	0&lt;BR /&gt;
386000	5/9/2010	10:00:00	12.12	0&lt;BR /&gt;
386000	5/2/2010	9:00:00	12.14	1&lt;BR /&gt;
386000	5/1/2010	8:00:00	12.13	0&lt;BR /&gt;
385000	3/3/2010	10:00:00	11.13	1&lt;BR /&gt;
385000	3/1/2010	8:00:00	11.12	0&lt;BR /&gt;
385000	3/1/2010	9:00:00	11.12	0&lt;BR /&gt;
&lt;BR /&gt;
For example,&lt;BR /&gt;
(1) For account 387000, since on 06/10/2010, the ID used is 13.15, it is different than the ID of 13.14 used on 06/08/2010 (which is within two days of 06/10/2010), so the hit is 1.&lt;BR /&gt;
&lt;BR /&gt;
(2) For Account 387000, since on 06/09/2010, the ID used is 13.15, it is different than the ID of 13.14 used on 06/08/2010(which is within two days of 06/09/2010), so the hit is 1.&lt;BR /&gt;
&lt;BR /&gt;
(3)For Account 387000, since on 06/04/2010 at 9:00am, the ID used is 13.13, within 2 days, the only date is another ID of same 13.13 on 06/04/2010 at time of 8:00am. The hit is 0.&lt;BR /&gt;
&lt;BR /&gt;
(4)For account 386000, within 2 days of 05/09/2010 would be 05/08/2010 or 05/07/2010. But there is no date likes these two, so the hit is 0.&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Thanks again.

Message was edited by: NIU_YA</description>
      <pubDate>Mon, 30 May 2011 23:24:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-using-SAS-Macro-to-create-complex-data-set/m-p/71323#M15423</guid>
      <dc:creator>NIUNIU</dc:creator>
      <dc:date>2011-05-30T23:24:16Z</dc:date>
    </item>
    <item>
      <title>Re: Help: using SAS Macro to create complex data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-using-SAS-Macro-to-create-complex-data-set/m-p/71324#M15424</link>
      <description>Ideally I think this should be done via Hash tables if the data can fit in memory.&lt;BR /&gt;
&lt;BR /&gt;
The method that comes to mind for me is a sql join on itself.&lt;BR /&gt;
&lt;BR /&gt;
I'm pretty sure the code won't work below but its the idea. This questions has been asked around health care data on this site so some googling might help find a closer solution. &lt;BR /&gt;
&lt;BR /&gt;
ie proc sql;&lt;BR /&gt;
create table test as&lt;BR /&gt;
select a.*, (select count(*) from have b where a.account=b.account and a.id=b.id and a.date-b.date&amp;lt;2) as hit&lt;BR /&gt;
from have a;&lt;BR /&gt;
quit;</description>
      <pubDate>Tue, 31 May 2011 04:16:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-using-SAS-Macro-to-create-complex-data-set/m-p/71324#M15424</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2011-05-31T04:16:03Z</dc:date>
    </item>
    <item>
      <title>Re: Help: using SAS Macro to create complex data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-using-SAS-Macro-to-create-complex-data-set/m-p/71325#M15425</link>
      <description>[pre]&lt;BR /&gt;
data temp;&lt;BR /&gt;
input account $ date : mmddyy10. time : time9. id ;&lt;BR /&gt;
format date mmddyy10. time time9.;&lt;BR /&gt;
datalines;&lt;BR /&gt;
387000 6/10/2010 12:00:00 13.15 1&lt;BR /&gt;
387000 6/9/2010 12:00:00 13.15 1&lt;BR /&gt;
387000 6/8/2010 12:00:00 13.14 1&lt;BR /&gt;
387000 6/6/2010 11:00:00 13.15 1&lt;BR /&gt;
387000 6/5/2010 10:00:00 13.14 1&lt;BR /&gt;
387000 6/4/2010 9:00:00 13.13 0&lt;BR /&gt;
387000 6/4/2010 8:00:00 13.13 0&lt;BR /&gt;
386000 5/9/2010 10:00:00 12.12 0&lt;BR /&gt;
386000 5/2/2010 9:00:00 12.14 1&lt;BR /&gt;
386000 5/1/2010 8:00:00 12.13 0&lt;BR /&gt;
385000 3/3/2010 10:00:00 11.13 1&lt;BR /&gt;
385000 3/1/2010 8:00:00 11.12 0&lt;BR /&gt;
385000 3/1/2010 9:00:00 11.12 0&lt;BR /&gt;
;&lt;BR /&gt;
run;&lt;BR /&gt;
proc sql;&lt;BR /&gt;
 create table want(drop=count) as&lt;BR /&gt;
  select  distinct a.* ,count(distinct b.date) as count ,case&lt;BR /&gt;
                                   when(calculated count =1) then 0&lt;BR /&gt;
                                   else 1&lt;BR /&gt;
                                 end as hit&lt;BR /&gt;
   from temp as a, temp as b&lt;BR /&gt;
    where a.account=b.account and  b.date between a.date-2 and a.date &lt;BR /&gt;
     group by a.account,a.date,a.id&lt;BR /&gt;
     ;&lt;BR /&gt;
quit;&lt;BR /&gt;
proc sort data=want ;&lt;BR /&gt;
 by  decending date;&lt;BR /&gt;
run;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Ksharp</description>
      <pubDate>Tue, 31 May 2011 08:36:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-using-SAS-Macro-to-create-complex-data-set/m-p/71325#M15425</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2011-05-31T08:36:39Z</dc:date>
    </item>
    <item>
      <title>Re: Help: using SAS Macro to create complex data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-using-SAS-Macro-to-create-complex-data-set/m-p/71326#M15426</link>
      <description>Thank you very much for your inputs.&lt;BR /&gt;
&lt;BR /&gt;
After i tried in the real data, I found the code should work after a slightly adjustment as below. &lt;BR /&gt;
&lt;BR /&gt;
select count(distinct b.id) as count &lt;BR /&gt;
&lt;BR /&gt;
It should not distinct on date. Let me know if my logic is wrong.&lt;BR /&gt;
&lt;BR /&gt;
Thanks again.&lt;BR /&gt;
&lt;BR /&gt;
Message was edited by: NIU_YA

Message was edited by: NIU_YA</description>
      <pubDate>Tue, 31 May 2011 16:01:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-using-SAS-Macro-to-create-complex-data-set/m-p/71326#M15426</guid>
      <dc:creator>NIUNIU</dc:creator>
      <dc:date>2011-05-31T16:01:54Z</dc:date>
    </item>
    <item>
      <title>Re: Help: using SAS Macro to create complex data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-using-SAS-Macro-to-create-complex-data-set/m-p/71327#M15427</link>
      <description>According to the logic you provided it should be a 1.&lt;BR /&gt;
&lt;BR /&gt;
ie same account number and same id and the dates are less than 2 days apart.  &lt;BR /&gt;
&lt;BR /&gt;
Is there some other logic?</description>
      <pubDate>Tue, 31 May 2011 17:41:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-using-SAS-Macro-to-create-complex-data-set/m-p/71327#M15427</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2011-05-31T17:41:08Z</dc:date>
    </item>
    <item>
      <title>Re: Help: using SAS Macro to create complex data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-using-SAS-Macro-to-create-complex-data-set/m-p/71328#M15428</link>
      <description>Yes.You are right.&lt;BR /&gt;
It should be b.id.&lt;BR /&gt;
It is typo mistake.Thank you point it out for me.&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Ksharp</description>
      <pubDate>Wed, 01 Jun 2011 11:05:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-using-SAS-Macro-to-create-complex-data-set/m-p/71328#M15428</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2011-06-01T11:05:13Z</dc:date>
    </item>
    <item>
      <title>Re: Help: using SAS Macro to create complex data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-using-SAS-Macro-to-create-complex-data-set/m-p/71329#M15429</link>
      <description>Hi &lt;BR /&gt;
&lt;BR /&gt;
I believe what Ksharp's code - with the little glitch -nicely demonstrates is that before you try and solve something with a macro you first get the code right for one of your cases - and only then generalise it via a macro.&lt;BR /&gt;
&lt;BR /&gt;
Also: If performance should become an issue AND the source tables are in SAS then a "hash approach" would perform better - but the code would also become quite a bit more complicated.&lt;BR /&gt;
&lt;BR /&gt;
Cheers&lt;BR /&gt;
Patrick

Message was edited by: Patrick</description>
      <pubDate>Wed, 01 Jun 2011 11:17:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-using-SAS-Macro-to-create-complex-data-set/m-p/71329#M15429</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2011-06-01T11:17:07Z</dc:date>
    </item>
    <item>
      <title>Re: Help: using SAS Macro to create complex data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-using-SAS-Macro-to-create-complex-data-set/m-p/71330#M15430</link>
      <description>Yes, I think you make the point. &lt;BR /&gt;
&lt;BR /&gt;
Thank you.</description>
      <pubDate>Thu, 02 Jun 2011 14:05:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-using-SAS-Macro-to-create-complex-data-set/m-p/71330#M15430</guid>
      <dc:creator>NIUNIU</dc:creator>
      <dc:date>2011-06-02T14:05:56Z</dc:date>
    </item>
    <item>
      <title>Re: Help: using SAS Macro to create complex data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-using-SAS-Macro-to-create-complex-data-set/m-p/71331#M15431</link>
      <description>Thank you for the inputs.&lt;BR /&gt;
&lt;BR /&gt;
Cheers,</description>
      <pubDate>Thu, 02 Jun 2011 14:06:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-using-SAS-Macro-to-create-complex-data-set/m-p/71331#M15431</guid>
      <dc:creator>NIUNIU</dc:creator>
      <dc:date>2011-06-02T14:06:48Z</dc:date>
    </item>
    <item>
      <title>Re: Help: using SAS Macro to create complex data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-using-SAS-Macro-to-create-complex-data-set/m-p/71332#M15432</link>
      <description>Thank you for the help.</description>
      <pubDate>Sat, 04 Jun 2011 18:22:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-using-SAS-Macro-to-create-complex-data-set/m-p/71332#M15432</guid>
      <dc:creator>NIUNIU</dc:creator>
      <dc:date>2011-06-04T18:22:05Z</dc:date>
    </item>
  </channel>
</rss>

