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

I am attempting to perform a count of deliveries for each AcctNo.

 

Here is the data set:

 

Brand   AcctNo   DeliveryDate  DeliveryUnits

   A        21120      7/31/20            100

   A        21120      8/7/20              150

   A        21120      8/14/20            200

   A        21120      8/21/20            100

   B        21121      8/5/20              350 

   B        21121      8/12/20            250

   B        21121      8/19/20            300

 

proc sql;
create table master as
select brand, count(distinct AcctNo) as accts format comma14.,
count(delivery_date) as drops format comma14., sum(DeliveryUnits) as drops_units format comma14.2
from minor
group by brand;
quit;

 

In this summary query, I want to count 4 deliveries for one AcctNo when grouping by brand which is why I used to distinct command to count each AcctNo only once. Unfortunately, the output gives me the exact same number for both counts. 

 

Any feedback would be greatly appreciated.  Thanks. 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Then your data is not as posted, or you ran a different code:

data minor;
input Brand $ AcctNo $ DeliveryDate :mmddyy10. DeliveryUnits;
format deliverydate yymmdd10.;
datalines;
   A        21120      7/31/20            100
   A        21120      8/7/20              150
   A        21120      8/14/20            200
   A        21120      8/21/20            100
   B        21121      8/5/20              350 
   B        21121      8/12/20            250
   B        21121      8/19/20            300
;

proc sql;
create table master as
  select
    brand,
    count(distinct AcctNo) as accts format comma14.,
    count(deliverydate) as drops format comma14.,
    sum(DeliveryUnits) as drops_units format comma14.2
  from minor
  group by brand
;
quit;

proc print noobs;
run;

Result:

Brand	accts	drops	drops_units
A	1	4	550.00
B	1	3	900.00

 

View solution in original post

4 REPLIES 4
mkeintz
PROC Star

@greg6363 wrote:

I am attempting to perform a count of deliveries for each AcctNo.

 

Here is the data set:

 

Brand   AcctNo   DeliveryDate  DeliveryUnits

   A        21120      7/31/20            100

   A        21120      8/7/20              150

   A        21120      8/14/20            200

   A        21120      8/21/20            100

   B        21121      8/5/20              350 

   B        21121      8/12/20            250

   B        21121      8/19/20            300

 

proc sql;
create table master as
select brand, count(distinct AcctNo) as accts format comma14.,
count(delivery_date) as drops format comma14., sum(DeliveryUnits) as drops_units format comma14.2
from minor
group by brand;
quit;

 

In this summary query, I want to count 4 deliveries for one AcctNo when grouping by brand which is why I used to distinct command to count each AcctNo only once. Unfortunately, the output gives me the exact same number for both counts. 

 

Any feedback would be greatly appreciated.  Thanks. 

 

 


I don't understand what you think is wrong - what values did you get, and what values did you expect?

 

--------------------------
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

--------------------------
greg6363
Obsidian | Level 7

This is the output I expect in the summary output:

Brand  Accts  Drops DropsUnits

   A          1        4         550

   B          1        3         900

 

This is the output I get from my summary output:

Brand  Accts  Drops DropsUnits

   A          4       4         550

   B          3       3         900

 

The Accts and Drops fields should not produce the same value when using the distinct option.

Kurt_Bremser
Super User

Then your data is not as posted, or you ran a different code:

data minor;
input Brand $ AcctNo $ DeliveryDate :mmddyy10. DeliveryUnits;
format deliverydate yymmdd10.;
datalines;
   A        21120      7/31/20            100
   A        21120      8/7/20              150
   A        21120      8/14/20            200
   A        21120      8/21/20            100
   B        21121      8/5/20              350 
   B        21121      8/12/20            250
   B        21121      8/19/20            300
;

proc sql;
create table master as
  select
    brand,
    count(distinct AcctNo) as accts format comma14.,
    count(deliverydate) as drops format comma14.,
    sum(DeliveryUnits) as drops_units format comma14.2
  from minor
  group by brand
;
quit;

proc print noobs;
run;

Result:

Brand	accts	drops	drops_units
A	1	4	550.00
B	1	3	900.00

 

greg6363
Obsidian | Level 7
Your code works but mine doesn't. Strange. Let me go back and review my data formats. Thanks.

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1182 views
  • 0 likes
  • 3 in conversation