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

Hi Team..could you please help me in building the logic. I have attached the sample file which is having sample input and the output  i am looking for

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User
%let maxcol=12;

data have;
length col_1-col_&maxcol $10;
input col_1-col_&maxcol;
cards;
Active Active Active Cancel Cancel Reactivate Active Active Active Active Active Active
Unknown Unknown Unknown Reactivate Active Active Active Active Active Cancel Cancel Cancel
Active Active Active Cancel Cancel Cancel Cancel Cancel Cancel Cancel Cancel Cancel
;
run;

data want;
set have;
array cols {*} col_1-col_&maxcol;
active_cancel = .;
cancel_reactivate = .;
unknown_reactivate = .;
*cancel_no_movement = .;
do i = 1 to &maxcol - 1;
  if cols{i} = "Active" and cols{i+1} = "Cancel" then active_cancel = 1;
  if cols{i} = "Cancel" and cols{i+1} = "Reactivate" then cancel_reactivate = 1;
  if cols{i} = "Unknown" and cols{i+1} = "Reactivate" then unknown_reactivate = 1;
end;
drop i;
run;

I omitted the fourth flag on purpose, as the original dataset does not have a corresponding value; I also think that active_cancel should be set in all three observations.

If not, you need to specify the rules; but I guess that from my example the technical handling of the array should be obvious.

View solution in original post

5 REPLIES 5
Jagadishkatam
Amethyst | Level 16
Could you please explain it further about the 4 new variables to be created depending on the existing variables.
Thanks,
Jag
SMohanReddy
Obsidian | Level 7

Hi..In the sample data each row is distinct for a customer..so customer 1 has a product A which is active for 3 months and then it was cancelled so a flag has to be raised as active_cancel = 1 and again he reactivated it so cancel_reactivate = 1. customer 2 with the same product A in the second row first its showing as Unknown and then it was reactivated so unknown_reactivate = 1.

please let me know if you need any details. 

thanks for the support

Kurt_Bremser
Super User
%let maxcol=12;

data have;
length col_1-col_&maxcol $10;
input col_1-col_&maxcol;
cards;
Active Active Active Cancel Cancel Reactivate Active Active Active Active Active Active
Unknown Unknown Unknown Reactivate Active Active Active Active Active Cancel Cancel Cancel
Active Active Active Cancel Cancel Cancel Cancel Cancel Cancel Cancel Cancel Cancel
;
run;

data want;
set have;
array cols {*} col_1-col_&maxcol;
active_cancel = .;
cancel_reactivate = .;
unknown_reactivate = .;
*cancel_no_movement = .;
do i = 1 to &maxcol - 1;
  if cols{i} = "Active" and cols{i+1} = "Cancel" then active_cancel = 1;
  if cols{i} = "Cancel" and cols{i+1} = "Reactivate" then cancel_reactivate = 1;
  if cols{i} = "Unknown" and cols{i+1} = "Reactivate" then unknown_reactivate = 1;
end;
drop i;
run;

I omitted the fourth flag on purpose, as the original dataset does not have a corresponding value; I also think that active_cancel should be set in all three observations.

If not, you need to specify the rules; but I guess that from my example the technical handling of the array should be obvious.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Yes, of course if its any occurence in that string, and its not too long you could simplfy it to a character search:

data want (drop=tmp);
  set have;
  length tmp $2000;
  array col_{12};
  tmp=cats(of col_{*});
  if index(tmp,"ActiveCancel") then active_cancel=1;
  if index(tmp,"CancelReactivate") then cancel_reactive=1;
  if index(tmp,"UnknownReactivate") then unknown_reactive=1;
run;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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
  • 1072 views
  • 1 like
  • 4 in conversation