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

Hello, 

 

I am new to using SAS and I having trouble with some data cleanup. Below is my dataset. I have cleaned up my original dataset to create a dataset that contains all duplicates for the reporting month, year and city_village.

 

Obs Reporting_YEAR Reporting_MONTH City_Village Sales_Total date123456
20196A$500001JUN2019
20196A$1001JUN2019
20197B$9001JUL2019
20197B$10001JUL2019
20198C$501AUG2019
20198C$601AUG2019

What I would like to do is merge together the sum of Sales_Total in one entry when the date and city_village are the same. For this example dataset I would like the following result. 

 

Obs Reporting_YEAR Reporting_MONTH City_Village Sales_Total date135
20196A$501001JUN2019
20197B$19001JUL2019
20198C$1101AUG2019

 

This is the code I have tried so far, instead of adding the sales total for duplicate year, month and city, it adds up the duplicate dates instead. For instance, some entries have different city_town for the date, so the code ends up merging according to the date as opposed to the same date and city_town.  I am not sure how to include an additional variable in the by-statement below.

 

proc summary data = all; 

by date;

var Sales_Total;

output out =want sum() = ;

run;

 

 

Thank you, any help would be appreciated. 

 

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

Try this

 

data have;
input Reporting_YEAR Reporting_MONTH City_Village $ Sales_Total :dollar6. date123456 :date9.;
format date123456 date9.;
datalines;
2019 6 A $5000 01JUN2019
2019 6 A $10 01JUN2019
2019 7 B $90 01JUL2019
2019 7 B $100 01JUL2019
2019 8 C $5 01AUG2019
2019 8 C $6 01AUG2019
;

proc summary data = have nway;
   class Reporting_YEAR Reporting_MONTH City_Village date123456 ;
   var Sales_Total;
   output out = want(drop = _:) sum=;
run;

View solution in original post

2 REPLIES 2
PeterClemmensen
Tourmaline | Level 20

Try this

 

data have;
input Reporting_YEAR Reporting_MONTH City_Village $ Sales_Total :dollar6. date123456 :date9.;
format date123456 date9.;
datalines;
2019 6 A $5000 01JUN2019
2019 6 A $10 01JUN2019
2019 7 B $90 01JUL2019
2019 7 B $100 01JUL2019
2019 8 C $5 01AUG2019
2019 8 C $6 01AUG2019
;

proc summary data = have nway;
   class Reporting_YEAR Reporting_MONTH City_Village date123456 ;
   var Sales_Total;
   output out = want(drop = _:) sum=;
run;
maliha9999
Calcite | Level 5

Thank you PeterClemmensen. The code worked and got me the dataset I wanted. 🙂

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 2 replies
  • 764 views
  • 3 likes
  • 2 in conversation