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
Amethyst | Level 16

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
Amethyst | Level 16

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.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

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