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

Dear all experts,

I have something as below:

 

TienHan83_0-1602492430263.png

For the same account, sorting by sr_month_key:

1. When acc_status = ACTIVE, then sr = 1. This is OK.

2. When acc_status = PARTIAL_DISCONNECT, then the rest of all rows for the same account_no, i want them to be 0, even though it changes back to ACTIVE.

 

How do I write this or do this in SAS EG? 

Please help, thanks.

1 ACCEPTED SOLUTION

Accepted Solutions
TienHan83
Fluorite | Level 6

Hi,

 

I've modified the script a bit and it is working fine now.

 

data want;
set have;
by account_no;
retain sr_var;
if first.account_no then sr_var=sr;
if acc_status = "PARTIAL DISCONNECT" then sr_var = 0;
sr=sr_var;
run;

 

Thank you for your guidance sir.

View solution in original post

6 REPLIES 6
Kurt_Bremser
Super User

You need to retain the new variable sr, and set it at the appropriate conditions.

data want;
set have;
by account_no;
retain sr;
if first.account_no then sr = 1;
if acc_status = "PARTIAL_DISCONNECT" then sr = 0;
run;

Untested. For tested code, supply example data in usable form (data step with datalines), and not AS A PICTURE!

It is never good to post data in pictures; typing data off a screenshot is not what we're here for.

TienHan83
Fluorite | Level 6

Hi Sir,

 

Apologies for my mistake.

Here is the sample data.

The ideal_sr column is my desired output.

 

account_nocm_month_keysr_month_keydate_diffacc_statuscust_sub_categorysrideal_sr
800512792019022019020ACTIVEPOSTPAID11
800512792019022019031ACTIVEPOSTPAID11
800512792019022019042ACTIVEPOSTPAID11
800512792019022019053ACTIVEPOSTPAID11
800512792019022019064ACTIVEPOSTPAID11
800512792019022019075ACTIVEPOSTPAID11
800512792019022019086ACTIVEPOSTPAID11
800512792019022019097ACTIVEPOSTPAID11
800512792019022019108ACTIVEPOSTPAID11
800512792019022019119ACTIVEPOSTPAID11
8005127920190220191210ACTIVEPOSTPAID11
8005127920190220200111PARTIAL DISCONNECTPOSTPAID00
8005127920190220200212ACTIVEPOSTPAID10
8005127920190220200313ACTIVEPOSTPAID10
8005127920190220200414ACTIVEPOSTPAID10
8005127920190220200515PARTIAL DISCONNECTPOSTPAID00
8005127920190220200616ACTIVEPOSTPAID10
8005127920190220200717ACTIVEPOSTPAID10
8005127920190220200818ACTIVEPOSTPAID10
8005127920190220200919ACTIVEPOSTPAID10
TienHan83
Fluorite | Level 6

Hi Sir, 

The results is not as expected. The values for sr remain the same as before...

Appreciate your guidance and thank you for your patience.

Kurt_Bremser
Super User

What do you mean with "before"? Does sr already exist in the incoming dataset?

I specifically mentioned that you have to create a new variable for RETAIN to have an effect.

If sr is part of the source dataset, you have to drop it with a dataset option:

set have (drop=sr);

Otherwise, please post the complete log of the code you ran, using the </> button.

TienHan83
Fluorite | Level 6

Hi,

 

I've modified the script a bit and it is working fine now.

 

data want;
set have;
by account_no;
retain sr_var;
if first.account_no then sr_var=sr;
if acc_status = "PARTIAL DISCONNECT" then sr_var = 0;
sr=sr_var;
run;

 

Thank you for your guidance sir.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 848 views
  • 3 likes
  • 2 in conversation