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

Good afternoon ! 

Currently got stuck with Proc SQL code. When I use group by operation, some missing rows are spoiling the output result.

According to SQL logic, I should use aggregation function (e.g. sum/avg) to collapse rows, but there is no aggregation function to exclude missing row from a group. 

 

Example:

Input data

IDCustomerDateSumField
ID1C105.05.202115AAA
ID2C105.05.202116 
ID3C105.05.202121AAA
ID4C221.05.202130BBB

 

I use this code:

proc sql noprint; 
  create table output as 
    select Customer, Date, Field
      ,sum(SUM) as Sum 
      ,count(*) as count
  from work.data_test
  group by Customer, Date, Field
;quit;

And get this:

CustomerDateSUMFieldcount
C105.05.202136AAA2
C105.05.202116 1
C221.05.202130BBB1

 

Instead I need to get this:

CustomerDateSumFieldcount
C105.05.202152AAA3
C221.05.202130BBB1

 

Is there any possible solution ?

 

Thanks 

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
proc sql noprint;
create table output as
select Customer, Date, max(Field) as Field
,sum(SUM) as Sum
,count(*) as count
from work.data_test
group by Customer, Date
;quit;

View solution in original post

5 REPLIES 5
sbxkoenk
SAS Super FREQ

The missing value for FIELD is of course a valid level, just like 'AAA' or 'BBB'.

If you don't want this, you have to impute the missing values.

You can for example do a Last Observation Carry Forward (LOCF = Forward Filling) to impute the missing value with a non-missing level.

Regards,

Koen

Reeza
Super User
Actually, you CAN use aggregation on character fields in SQL but not if you're aggregating by that variable as well. So do you actually need to aggregate by FIELD? How do you know what value it should be?
Ksharp
Super User
proc sql noprint;
create table output as
select Customer, Date, max(Field) as Field
,sum(SUM) as Sum
,count(*) as count
from work.data_test
group by Customer, Date
;quit;
Tom
Super User Tom
Super User

What is the logic that says the missing FIELD should be replace by AAA in your example?

Why not include it as FIELD='BBB' or 'CCC'?

 

Banker337
Fluorite | Level 6
Same customer and date

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 5 replies
  • 582 views
  • 3 likes
  • 5 in conversation