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

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

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

2 REPLIES 2
Astounding
PROC Star

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.

Mirisage
Obsidian | Level 7

Hi Astounding,

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

Regards

Mirisa

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 2 replies
  • 778 views
  • 0 likes
  • 2 in conversation