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

Hi,

My task is to identify clients who spread the payment over time. If the event occurs, the value is 1, otherwise 0. I'm working in SAS guide so I prefer solution in query builder. I got example data table:

 

Customer MAY JUNE JULY Target

A              100    0        200     1

B              300    0        0         0

 

How can I achieve this?

1 ACCEPTED SOLUTION

Accepted Solutions
yabwon
Onyx | Level 15

simple task for array,

 

data have;
input Customer $ MAY JUNE JULY;
cards;
A              100    0        200 
B              300    0        0   
;
run;

data want;
  set have;
  array months MAY JUNE JULY;
  target =0;
  do over months; 
    target + (months > 0);
  end;
  target = (target > 1);
run;

but I would suggest to keep data in "long" format:

 

data have_long;
input Customer $ months $ amount;
cards;
A MAY 100
A JUNE 0
A JULY 200
B MAY 300
B JUNE 0
B JULY 0
;
run;

proc sql;
  select Customer, 
  case when sum(amount > 0) > 1 then 1
                                else 0
  enD as target
  from
    have_long
  group by 
    Customer
  ;
quit;

 

All the best

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



View solution in original post

5 REPLIES 5
yabwon
Onyx | Level 15

simple task for array,

 

data have;
input Customer $ MAY JUNE JULY;
cards;
A              100    0        200 
B              300    0        0   
;
run;

data want;
  set have;
  array months MAY JUNE JULY;
  target =0;
  do over months; 
    target + (months > 0);
  end;
  target = (target > 1);
run;

but I would suggest to keep data in "long" format:

 

data have_long;
input Customer $ months $ amount;
cards;
A MAY 100
A JUNE 0
A JULY 200
B MAY 300
B JUNE 0
B JULY 0
;
run;

proc sql;
  select Customer, 
  case when sum(amount > 0) > 1 then 1
                                else 0
  enD as target
  from
    have_long
  group by 
    Customer
  ;
quit;

 

All the best

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



Kurt_Bremser
Super User

If you want to do this in the query builder, you have to transpose first.

Otherwise you need a manually written data step with array processing, as shown by @yabwon 

FreelanceReinh
Jade | Level 19

Hi @PatrykSAS,

 

Would a single numeric expression be more amenable to the Query Builder? (I don't know as I don't use EG.) If so, you could try something like this:

target=largest(2, of may--july)>0;

Note that the inequality is equivalent to "There are at least two positive numbers among the values of the variables in the name range list may--july in the current observation."

TomKari
Onyx | Level 15

Using the "point and click" facilities takes several steps, but each one is fairly easy.

 

1. First, sort by Customer (needed for the transpose).

 

2. Then run a Data -> Transpose that has Customer as the "Group Analysis By" variable, and all of the other variables as the transpose variables. The result should look like this:

 

Customer NAME OF FORMER VARIABLE Column1
A MAY 100
A JUNE 0
A JULY 200
B MAY 300
B JUNE 0
B JULY 0

 

3. Now, run a query to select only the rows where Column1 is greater than zero (get rid of your zero values).

 

4. Finally, you can either use a query that counts the number of records for each customer, or use Describe -> Summary Statistics to find the customers that have count 1, or greater than 1.

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!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 3028 views
  • 2 likes
  • 5 in conversation