Help using Base SAS procedures

Forcing a condition along each observation

Accepted Solution Solved
Reply
Super Contributor
Posts: 338
Accepted Solution

Forcing a condition along each observation

Hi SAS Forum;

I have the following dataset.

data have;

input ID status0 $ 5-11 status1 $ 13-19 status2 $ 21-27 status3 $ 29-35 status4 $ 37-43;

cards;

111 current current current current current

222 npna    dwo     dwo     1-30    90+   

333 current dwo     31-60   61-90   dwo

;

run;

What I wanted:

I wanted to force this condition for each and every observation.

Condition is: if an account hits "dwo" state at any point from status0 to status4, then the remainder of the statuses also should be "dwo"

Answer:

IDstatus0status1status2status3status4
111currentcurrentcurrentcurrentcurrent
222npnadwodwodwodwo
333currentdwodwodwodwo

My code below does do the job correctly, but could any expert suggest me an alternative elagont code (my original dataset has 24 statuses such as status0, status1, ......status24 which neccesitate me to write a long code if I use my long coding appraoch below).

.

data want2;

set have;

if status0 in ("dwo") then do;

status1= "dwo";

status2= "dwo";

status3= "dwo";

status4= "dwo";

end;

if status1 in ("dwo") then do;

status2= "dwo";

status3= "dwo";

status4= "dwo";

end;

if status2 in ("dwo") then do;

status3= "dwo";

status4= "dwo";

end;

if status3 in ("dwo") then do;

status4= "dwo";

end;

run;

Thanks

Mirisa


Accepted Solutions
Solution
‎03-31-2015 11:44 AM
Super User
Posts: 5,099

Re: Forcing a condition along each observation

Mirisa,

In general, when you have a set of variables that should be processed in very similar fashion, arrays are often a good tool for the job.  Here's how the program might look:

data want;

set have;

array all5 {5} status0-status4;

do i=2 to 5;

    if all5{i-1} = "dwo" then all5{i}="dwo";

end;

run;

It should be easy to expand this to 25 variables instead of 5.

Good luck.

View solution in original post


All Replies
Solution
‎03-31-2015 11:44 AM
Super User
Posts: 5,099

Re: Forcing a condition along each observation

Mirisa,

In general, when you have a set of variables that should be processed in very similar fashion, arrays are often a good tool for the job.  Here's how the program might look:

data want;

set have;

array all5 {5} status0-status4;

do i=2 to 5;

    if all5{i-1} = "dwo" then all5{i}="dwo";

end;

run;

It should be easy to expand this to 25 variables instead of 5.

Good luck.

Super Contributor
Posts: 338

Re: Forcing a condition along each observation

Hi Astounding,

Thank you very much for your code. It worked well.

Regards

Mirisa

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 181 views
  • 0 likes
  • 2 in conversation