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

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

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 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

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.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

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

View all other training opportunities.

Discussion stats
  • 2 replies
  • 202 views
  • 3 likes
  • 2 in conversation