BookmarkSubscribeRSS Feed
Neeydchi
Fluorite | Level 6

 

I need to select the customers who made purchases according to the criteria of each of my MASTER_ID -and save it in a table for each of my MASTER_ID 

 

I have the following  matrix (MATRIX_TABLE): 

MASTER_IDCAMPAIGNSPRODUCTSTART_DATEEND_DATE
1K1PROD_88T01/01/202105/01/2021
2k1PROD_55X20/12/202020/02/2021
3k2PROD_75A15/01/202115/03/2021
4K1PROD_95Y01/04/202114/04/2021

 

OBS: The same campaign can be repeated in several lines, since the same campaign can encompass several different products and different start and end dates.

 

And I have the following table that lists all the purchases my customers made during my campaigns.

 

PURCHASES ( PURCHASES_TABLE)

CLIENT_IDCLIENT_NAMEPRODUCTAMOUNT_PURCHASEPURCHASE_DATE
CLI_1CLIENT 1PROD_95Y20001/10/2020
CLI_2CLIENT 2PROD_88T30015/02/2021
CLI_3CLIENT 3PROD_95Y10011/01/2021

 

My code runs, but I'm not getting my expected result. I generate three tables (PURCHASES_ & MASTER_ID), but the results are the same in the three tables. That is, it does not select the criteria according to each MASTER_ID

 

This is my attempt to get the purchases of the K1 Campaign:

 

%global campaign;

%macro DDC (campaign);
PROC SQL noprint;

SELECT count(*)
INTO :n_var
FROM MATRIX_TABLE;
QUIT;

%DO I = 1 %TO &n_var;

data _null_;
set WORK.MATRIX_TABLE;

if _n_= &I;
call symput('campaign', campaign);
call symput('MASTER_ID', MASTER_ID);

run;

PROC SQL;
CREATE TABLE PURCHASES_&MASTER_ID AS
SELECT t2.CLIENT_ID, T2.CLIENT_NAME, t1.PRODUCT
FROM matrix_table t1, PURCHASES_TABLE T2
WHERE t1.PRODUCT = T2.PRODUCT
and t2.PURCHASE_DATE >= t1.START_DATE
and t2.PURCHASE_DATE <= t1.END_DATE;
QUIT;

%end;

%mend DDC;

%DDC (K1);

 

 

 

 

 

 

5 REPLIES 5
ballardw
Super User

Can you state the criteria in words and show what you expect for the output given the example data?

Neeydchi
Fluorite | Level 6
EXPECTED OUTPUT FOR %DDC (K1);

TABLE PURCHASES_1 ( PURCHASES_&MASTER_ID):
CLIENT_ID, CLIENT_NAME, PRODUCT for all customers who purchased "PROD_88T" between "01/01/2021" and "05/01/2021"

TABLE PURCHASES_2 ( PURCHASES_&MASTER_ID):
CLIENT_ID, CLIENT_NAME, PRODUCT for all customers who purchased " PROD_55X" between "20/12/2020" and "20/02/2021"

TABLE PURCHASES_4 ( PURCHASES_&MASTER_ID):
CLIENT_ID, CLIENT_NAME, PRODUCT for all customers who purchased " PROD_95Y" between "01/04/2021" and "14/04/2021"
maguiremq
SAS Super FREQ

First, @ballardw is asking what you want the data sets to look like. Second, your CALL SYMPUT has trailing blanks when I run it, unless I made a mistake somewhere. Third, is there an actual difference between 'k1' and 'K1'? Fourth, is there a reason you just have a numeric identifier for the data sets as opposed to the campaigns identifier?

I'm thinking that this can be streamlined and more data-driven, but there are a lot of unknowns out there.

ballardw
Super User

@Neeydchi wrote:
EXPECTED OUTPUT FOR %DDC (K1);

TABLE PURCHASES_1 ( PURCHASES_&MASTER_ID):
CLIENT_ID, CLIENT_NAME, PRODUCT for all customers who purchased "PROD_88T" between "01/01/2021" and "05/01/2021"

TABLE PURCHASES_2 ( PURCHASES_&MASTER_ID):
CLIENT_ID, CLIENT_NAME, PRODUCT for all customers who purchased " PROD_55X" between "20/12/2020" and "20/02/2021"

TABLE PURCHASES_4 ( PURCHASES_&MASTER_ID):
CLIENT_ID, CLIENT_NAME, PRODUCT for all customers who purchased " PROD_95Y" between "01/04/2021" and "14/04/2021"

Since you are showing dates with quotes then that appears the "date" is a character value and "between" means alphabetically not chronologically. At least from what you show. So between "20/12/2020" and "20/02/2021" does not include '01/01/2021' because '01' is not greater than "20" at the start of the string. If your data has character values for dates then the first step is convert them to SAS data values.

Reeza
Super User
I would have expected to see a filter somewhere in that query for the campaign, K1, but I don't.

Can you show a non macro query that does generate what you want?

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 786 views
  • 0 likes
  • 4 in conversation