BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
shlomiohana
Obsidian | Level 7

Hello,

I have the following table named CALLS:

shlomiohana_0-1653848377192.png

 

The code to create the table:

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;

I have 2 questions:


1. Who are the employees who made the same number of calls that day?

The output:

shlomiohana_0-1653856517479.png


2. Add to the table CALLS a field that shows the number of calls the day before.

The output:

shlomiohana_1-1653856814592.png

 

I need to answer 2 questions in a code, preferably in sql code.

 

Thanks.

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
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 > 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;

View solution in original post

7 REPLIES 7
PaigeMiller
Diamond | Level 26

Don't do this in SQL.

 

Question 1:

 

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;

 

Question 2:

 

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;

 

--
Paige Miller
mkeintz
PROC Star

What if you have multple sets of tied number of calls on a given day?  Say two people have 67 calls and two others have 59 calls.

 

Also, please show what your desired output should look like.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
shlomiohana
Obsidian | Level 7

I edited the post, now there is output.

mkeintz
PROC Star

You apparently want the output base on the names that have tied calls in the form of a report, not a dataset.   

 

But what if you have multiple ties on the same day, but at different levels?

 

What if you have ties on different days?   

 

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.  They just know two names.  They don't know at what level of the tie took place, and they don't know when it occurred.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Ksharp
Super User
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 > 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;
Ksharp
Super User
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;
shlomiohana
Obsidian | Level 7

You are amazing, thank you so much for the types of solution

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 7 replies
  • 585 views
  • 0 likes
  • 4 in conversation