<?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: how to write sql code in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/how-to-write-sql-code/m-p/815682#M321944</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data CALLS;
 input FIRST_NAME $ DAY_OF_MONTH :ddmmyy10. NUM_OF_CALLS;
 format DAY_OF_MONTH ddmmyy10.;
 datalines;
TONY 01/01/2021 75
STEVE 01/01/2021 61
NATASHA 01/01/2021 50
TONY 02/01/2021 64
STEVE 02/01/2021 80
NATASHA 02/01/2021 100
TONY 03/01/2021 33
STEVE 03/01/2021 99
NATASHA 03/01/2021 91
TONY 04/01/2021 70
STEVE 04/01/2021 71
NATASHA 04/01/2021 70
;
run;

proc sql;
create table want1 as
select *
 from CALLS
  group by DAY_OF_MONTH,NUM_OF_CALLS
   having count(*) ne 1;

create table want2 as
select a.*,b.NUM_OF_CALLS as NUM_OF_CALLS_before
 from CALLS as a left join CALLS as b
  on a.FIRST_NAME=b.FIRST_NAME and a.DAY_OF_MONTH &amp;gt; b.DAY_OF_MONTH 
   group by a.FIRST_NAME,a.DAY_OF_MONTH,a.NUM_OF_CALLS
    having a.DAY_OF_MONTH - b.DAY_OF_MONTH = min(a.DAY_OF_MONTH - b.DAY_OF_MONTH)
     order by 2,1;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 30 May 2022 12:15:53 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2022-05-30T12:15:53Z</dc:date>
    <item>
      <title>how to write sql code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-write-sql-code/m-p/815628#M321918</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;I have the following table named CALLS:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="shlomiohana_0-1653848377192.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/71865i85D8DA2DF847E90D/image-size/medium?v=v2&amp;amp;px=400" role="button" title="shlomiohana_0-1653848377192.png" alt="shlomiohana_0-1653848377192.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The code to create the table:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data CALLS;
	input FIRST_NAME $ DAY_OF_MONTH :ddmmyy10. NUM_OF_CALLS;
	format DAY_OF_MONTH ddmmyy10.;
	datalines;
TONY 01/01/2021 75
STEVE 01/01/2021 61
NATASHA 01/01/2021 50
TONY 02/01/2021 64
STEVE 02/01/2021 80
NATASHA 02/01/2021 100
TONY 03/01/2021 33
STEVE 03/01/2021 99
NATASHA 03/01/2021 91
TONY 04/01/2021 70
STEVE 04/01/2021 71
NATASHA 04/01/2021 70
;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I have 2 questions:&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;1. Who are the employees who made the same number of calls that day?&lt;/P&gt;
&lt;P&gt;&lt;U&gt;The output:&lt;/U&gt;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="shlomiohana_0-1653856517479.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/71866i3713F003B14585F1/image-size/medium?v=v2&amp;amp;px=400" role="button" title="shlomiohana_0-1653856517479.png" alt="shlomiohana_0-1653856517479.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;2. Add to the table CALLS a field that shows the number of calls the day before.&lt;/P&gt;
&lt;P&gt;&lt;U&gt;The output:&lt;/U&gt;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="shlomiohana_1-1653856814592.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/71867i5EF0118191A9FB70/image-size/medium?v=v2&amp;amp;px=400" role="button" title="shlomiohana_1-1653856814592.png" alt="shlomiohana_1-1653856814592.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I need to answer 2 questions in a code, preferably&amp;nbsp;in sql code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks.&lt;/P&gt;</description>
      <pubDate>Sun, 29 May 2022 20:42:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-write-sql-code/m-p/815628#M321918</guid>
      <dc:creator>shlomiohana</dc:creator>
      <dc:date>2022-05-29T20:42:19Z</dc:date>
    </item>
    <item>
      <title>Re: how to write sql code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-write-sql-code/m-p/815629#M321919</link>
      <description>&lt;P&gt;Don't do this in SQL.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Question 1:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have;
    by num_of_calls;
run;
data want1;
    set have;
    prev_num_of_calls=lag(num_of_calls);
    prev_first_name=lag(first_name);
    if prev_num_of_calls=num_of_calls then 
        put 'Answer to Q1: ' first_name prev_first_name;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Question 2:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=have nway;
    class day_of_month;
    var num_of_calls;
    output out=_stats_ sum=sum_num_of_calls;
run;
data previous_day;
    set _stats_;
    day_of_month=day_of_month+1;
run;
data want;
    merge have previous_day(rename=(sum_num_of_calls=prev_day_sum_num_of_calls));
    by day_of_month;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 29 May 2022 18:39:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-write-sql-code/m-p/815629#M321919</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-05-29T18:39:12Z</dc:date>
    </item>
    <item>
      <title>Re: how to write sql code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-write-sql-code/m-p/815630#M321920</link>
      <description>&lt;P&gt;What if you have multple sets of tied number of calls on a given day?&amp;nbsp; Say two people have 67 calls and two others have 59 calls.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, please show what your desired output should look like.&lt;/P&gt;</description>
      <pubDate>Sun, 29 May 2022 20:24:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-write-sql-code/m-p/815630#M321920</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2022-05-29T20:24:04Z</dc:date>
    </item>
    <item>
      <title>Re: how to write sql code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-write-sql-code/m-p/815633#M321921</link>
      <description>&lt;P&gt;I edited the post, now there is output.&lt;/P&gt;</description>
      <pubDate>Sun, 29 May 2022 20:45:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-write-sql-code/m-p/815633#M321921</guid>
      <dc:creator>shlomiohana</dc:creator>
      <dc:date>2022-05-29T20:45:07Z</dc:date>
    </item>
    <item>
      <title>Re: how to write sql code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-write-sql-code/m-p/815636#M321924</link>
      <description>&lt;P&gt;You apparently want the output base on the names that have tied calls in the form of a report, not a dataset.&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But what if you have multiple ties on the same day, but at different levels?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What if you have ties on different days?&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your simple posting of a pair of names that have the only tie in your sample data would not help the consumer of your report to know what they are looking at.&amp;nbsp; They just know two names.&amp;nbsp; They don't know at what level of the tie took place, and they don't know when it occurred.&lt;/P&gt;</description>
      <pubDate>Sun, 29 May 2022 22:43:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-write-sql-code/m-p/815636#M321924</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2022-05-29T22:43:40Z</dc:date>
    </item>
    <item>
      <title>Re: how to write sql code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-write-sql-code/m-p/815682#M321944</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data CALLS;
 input FIRST_NAME $ DAY_OF_MONTH :ddmmyy10. NUM_OF_CALLS;
 format DAY_OF_MONTH ddmmyy10.;
 datalines;
TONY 01/01/2021 75
STEVE 01/01/2021 61
NATASHA 01/01/2021 50
TONY 02/01/2021 64
STEVE 02/01/2021 80
NATASHA 02/01/2021 100
TONY 03/01/2021 33
STEVE 03/01/2021 99
NATASHA 03/01/2021 91
TONY 04/01/2021 70
STEVE 04/01/2021 71
NATASHA 04/01/2021 70
;
run;

proc sql;
create table want1 as
select *
 from CALLS
  group by DAY_OF_MONTH,NUM_OF_CALLS
   having count(*) ne 1;

create table want2 as
select a.*,b.NUM_OF_CALLS as NUM_OF_CALLS_before
 from CALLS as a left join CALLS as b
  on a.FIRST_NAME=b.FIRST_NAME and a.DAY_OF_MONTH &amp;gt; b.DAY_OF_MONTH 
   group by a.FIRST_NAME,a.DAY_OF_MONTH,a.NUM_OF_CALLS
    having a.DAY_OF_MONTH - b.DAY_OF_MONTH = min(a.DAY_OF_MONTH - b.DAY_OF_MONTH)
     order by 2,1;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 30 May 2022 12:15:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-write-sql-code/m-p/815682#M321944</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2022-05-30T12:15:53Z</dc:date>
    </item>
    <item>
      <title>Re: how to write sql code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-write-sql-code/m-p/815683#M321945</link>
      <description>&lt;PRE&gt;data CALLS;
 input FIRST_NAME $ DAY_OF_MONTH :ddmmyy10. NUM_OF_CALLS;
 format DAY_OF_MONTH ddmmyy10.;
 datalines;
TONY 01/01/2021 75
STEVE 01/01/2021 61
NATASHA 01/01/2021 50
TONY 02/01/2021 64
STEVE 02/01/2021 80
NATASHA 02/01/2021 100
TONY 03/01/2021 33
STEVE 03/01/2021 99
NATASHA 03/01/2021 91
TONY 04/01/2021 70
STEVE 04/01/2021 71
NATASHA 04/01/2021 70
;
run;

proc sql;
create table want1 as
select *
 from CALLS
  group by DAY_OF_MONTH,NUM_OF_CALLS
   having count(*) ne 1;
quit;

data want2;
if _n_=1 then do;
 if 0 then set CALLS(rename=(NUM_OF_CALLS=NUM_OF_CALLS_before)) ;
 declare hash h();
 h.definekey('FIRST_NAME');
 h.definedata('NUM_OF_CALLS_before');
 h.definedone();
end;
set CALLS;
if h.find()=0 then h.replace(key:FIRST_NAME,data:NUM_OF_CALLS);
 else do;h.add(key:FIRST_NAME,data:NUM_OF_CALLS);NUM_OF_CALLS_before=.;end;
run;&lt;/PRE&gt;</description>
      <pubDate>Mon, 30 May 2022 12:16:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-write-sql-code/m-p/815683#M321945</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2022-05-30T12:16:45Z</dc:date>
    </item>
    <item>
      <title>Re: how to write sql code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-write-sql-code/m-p/815737#M321966</link>
      <description>&lt;P&gt;You are amazing, thank you so much for the types of solution&lt;/P&gt;</description>
      <pubDate>Mon, 30 May 2022 19:59:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-write-sql-code/m-p/815737#M321966</guid>
      <dc:creator>shlomiohana</dc:creator>
      <dc:date>2022-05-30T19:59:05Z</dc:date>
    </item>
  </channel>
</rss>

