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

Hello;

I have a data set which shows the sales data over a period of time in different zip codes.  In a second data set, I have the purchase date of the product for each customer.

I want to calculate the total sales from the beginning of time until the month before each customer has purchased the product in each zip code; in other words, I want to know the aggregate number of  people had purchased the product in the vicinity (zip code)  of each customer until the month before he/she purchased the product. ; I appreciate if anyone can give me a hint of how I can do this.

Thanks for reading this post.

the data sets are like this:

Sales data set:

*Please note that all of the missing observations in this data set should be considered zero;

data sales;

input obs zipcode  jan10 feb10 mar10 apr10 mar10 jun10 july10 aug10 sep10 oct10 nov10 dec10 jan11 feb11;

data lines;

     1         14001  1  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .

    2         14009  1  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .

    3         14011  .  1  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .

    4         14020  .  .  1  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .

    5         14026  0  0  0  1  0  0  0  0  0  0  0  0  0  0  0  0  0  0

    6         14031  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .

    7         14036  .  .  1  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .

    8         14043  .  .  .  .  .  .  .  .  .  .  .  .  1  1  .  .  .  .

    9         14047  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .

   10         14048  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .;

run;

purchase data set:

data purchasedate;

input customername $ customerzipcode  purchasedate $;

datalines;

Customer1  14001 July10

customer2   14043  Jan11

....

run;

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

My code assumes that the columns in the sales table are in chronological order, as they were in your example. - PG

PG

View solution in original post

3 REPLIES 3
PGStats
Opal | Level 21

It can be done this way :

data sales;
input obs zipcode  jan10 feb10 mar10 apr10 may10 jun10 july10 aug10 sep10
     oct10 nov10 dec10 jan11 feb11 mar11 apr11 may11 jun11;
datalines;
    1         14001  1  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
    2         14009  1  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
    3         14011  .  1  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
    4         14020  .  .  1  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
    5         14026  0  0  0  1  0  0  0  0  0  0  0  0  0  0  0  0  0  0
    6         14031  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
    7         14036  .  .  1  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
    8         14043  .  .  .  .  1  1  .  .  .  .  .  .  .  .  .  .  .  .
    9         14047  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
   10         14048  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
;

proc sort data=sales; by zipcode; run;

data purchasedate;
length customerName $12;
input customerName $ customerZipcode  purchaseDate $;
datalines;
Customer1  14001 July10
customer2   14043  Jan11
;

proc sort data=purchaseDate; by customerZipcode customerName; run;

data previousSales(keep=customerName zipcode purchaseDate cumSales);
if 0 then set sales; /* Do nothing, get the variable names from sales to form array s */
array s{*} _numeric_;
merge purchaseDate(in=ok rename=customerZipcode=zipcode) sales;
by zipcode;
if ok then do;
     cumSales = 0;
     do i = 3 to dim(s) while (upcase(vname(s{i})) ne upcase(purchaseDate));
          cumSales + s{i};
          end;
     output;
     end;
run;
 
proc print; run;

PG

PG
niam
Quartz | Level 8

Thanks for the quick reply.

I tried your method, but I think something is going wrong with the sas date values, the results do not seem correct.

This is a part of the real data, maybe it helps;

customerpurchasedateZip
customer14/22/200914227
customer211/29/201014001
customer311/3/200914217
customer411/18/201014051
customer54/19/201014224
customer64/2/200914120
customer72/8/201114011
customer86/3/201014052
customer96/19/200914569
customer104/6/201014221
customer116/11/201014226
customer1210/5/201014221
customer1311/11/201014224
customer146/12/200914221
customer1511/1/201014215
customer165/10/201014226
customer1710/28/200914224
customer186/3/201014215
customer195/15/200914221
customer207/16/201014209
customer218/27/200714215
customer225/9/200914225
customer239/16/200914141
customer2411/19/201014569
customer256/18/200914224
customer264/30/200914224
customer272/17/201114127
customer285/1/200914224
customer2912/10/201014209
customer307/17/200914216

ZipNOV2010FEB2011JUL2010JUL2009AUG2009SEP2009OCT2009NOV2009DEC2009JAN2010FEB2010MAR2010APR2010MAY2010
140011
140091
140111
140201
1402600010000000000
14031
140361
1404311
14047
14048
14051101000
140521000001000000
14063
140701
1407500010000000000
140861010000000000
14092
1409401
14103
141200000100000020
1412711000020101000
141411
14150100000000000
141721
1420100000001
1420200000000000010
1420300001000010000
14204
142071100000000
PGStats
Opal | Level 21

My code assumes that the columns in the sales table are in chronological order, as they were in your example. - PG

PG

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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