Prev: MACROS IN SAS.
Next: Mean(x,y) vs (x+y)/2
From: Harshal on 24 Apr 2010 09:47 Dear All, I have a data which looks like this ID Status 1 1 1 2 1 2 1 2 2 1 2 2 2 2 3 1 3 1 3 1 If any ID has two consecutive "2" as their status anywhere, then that person would be a case. I want a new variable which can give me the case=1 and controls(those who to not have two consecutive 2's)=0. Is there any simple way of doing this? I am new to programming and your help will be appreciated. regards, Harshal
From: Arthur Tabachneck on 24 Apr 2010 10:11 Harshal, There are probably many ways to do what you want. One way, shown below, uses what is known as a DOW loop to pass through the data twice: once to test for your condition and the other to output the records with the new field added: data have; input ID Status; cards; 1 1 1 2 1 2 1 2 2 1 2 2 2 2 3 1 3 1 3 1 ; data want; do until(last.id); set have; by id; if Status*lag(Status) eq 4 then case=1; end; do until(last.id); set have; by id; if case ne 1 then case=0; output; end; run; On Apr 24, 9:47 am, Harshal <harshaldeshm...(a)gmail.com> wrote: > Dear All, > > I have a data which looks like this > > ID Status > 1 1 > 1 2 > 1 2 > 1 2 > 2 1 > 2 2 > 2 2 > 3 1 > 3 1 > 3 1 > > If any ID has two consecutive "2" as their status anywhere, then that > person would be a case. I want a new variable which can give me the > case=1 and controls(those who to not have two consecutive 2's)=0. > > Is there any simple way of doing this? > > I am new to programming and your help will be appreciated. > > regards, > Harshal
From: Harshal on 24 Apr 2010 11:02 Thank you so much Arthur! This a big help. It worked! Just one more question. If I wanted anyone to be case who has atleast 2 observations of "2" in the status (doesn't matter if they are not consecutive), then how how would I change this code? regards, Harshal On Apr 24, 3:11 pm, Arthur Tabachneck <art...(a)netscape.net> wrote: > Harshal, > > There are probably many ways to do what you want. One way, shown > below, uses what is known as a DOW loop to pass through the data > twice: once to test for your condition and the other to output the > records with the new field added: > > data have; > input ID Status; > cards; > 1 1 > 1 2 > 1 2 > 1 2 > 2 1 > 2 2 > 2 2 > 3 1 > 3 1 > 3 1 > ; > data want; > do until(last.id); > set have; > by id; > if Status*lag(Status) eq 4 then case=1; > end; > > do until(last.id); > set have; > by id; > if case ne 1 then case=0; > output; > end; > run; > > On Apr 24, 9:47 am, Harshal <harshaldeshm...(a)gmail.com> wrote: > > > > > Dear All, > > > I have a data which looks like this > > > ID Status > > 1 1 > > 1 2 > > 1 2 > > 1 2 > > 2 1 > > 2 2 > > 2 2 > > 3 1 > > 3 1 > > 3 1 > > > If any ID has two consecutive "2" as their status anywhere, then that > > person would be a case. I want a new variable which can give me the > > case=1 and controls(those who to not have two consecutive 2's)=0. > > > Is there any simple way of doing this? > > > I am new to programming and your help will be appreciated. > > > regards, > > Harshal
From: Barry Schwarz on 24 Apr 2010 13:09 This is not do-my-homework-for-me. You have to at least try. Arthur showed you how to set a status for two consecutive observations with 2. How would you change the one statement to keep track of the total number of observations with 2? Obviously you would no longer care about the previous observation. What part of the statement references the previous observation? Just as obviously, you would not want to set case to the same value on the second observation with a 2 as you did for the first value. What SAS construct lets you a accumulate a total? After accumulating a count for the number of observations with 2, what is the appropriate test to determine the final value of case that gets output? On Sat, 24 Apr 2010 08:02:11 -0700 (PDT), Harshal <harshaldeshmukh(a)gmail.com> wrote: >Thank you so much Arthur! This a big help. It worked! > >Just one more question. If I wanted anyone to be case who has atleast >2 observations of "2" in the status (doesn't matter if they are not >consecutive), then how how would I change this code? > >regards, >Harshal > > > >On Apr 24, 3:11�pm, Arthur Tabachneck <art...(a)netscape.net> wrote: >> Harshal, >> >> There are probably many ways to do what you want. �One way, shown >> below, uses what is known as a DOW loop to pass through the data >> twice: once to test for your condition and the other to output the >> records with the new field added: >> >> data have; >> � input ID �Status; >> � cards; >> 1 �1 >> 1 �2 >> 1 �2 >> 1 �2 >> 2 �1 >> 2 �2 >> 2 �2 >> 3 �1 >> 3 1 >> 3 1 >> ; >> data want; >> � do until(last.id); >> � � set have; >> � � by id; >> � � if Status*lag(Status) eq 4 then case=1; >> � end; >> >> � do until(last.id); >> � � set have; >> � � by id; >> � � if case ne 1 then case=0; >> � � output; >> � end; >> run; >> >> On Apr 24, 9:47�am, Harshal <harshaldeshm...(a)gmail.com> wrote: >> >> >> >> > Dear All, >> >> > I have a data which looks like this >> >> > ID �Status >> > 1 �1 >> > 1 �2 >> > 1 �2 >> > 1 �2 >> > 2 �1 >> > 2 �2 >> > 2 �2 >> > 3 �1 >> > 3 1 >> > 3 1 >> >> > If any ID has two consecutive �"2" as their status anywhere, then that >> > person would be a case. I want a new variable which can give me the >> > case=1 and controls(those who to not have two consecutive 2's)=0. >> >> > Is there any simple way of doing this? >> >> > I am new to programming and your help will be appreciated. >> >> > regards, >> > Harshal -- Remove del for email
From: Arthur Tabachneck on 24 Apr 2010 17:14 Harshal, On the listserv side of SAS-L Mike Zdeb pointed out that my proposed code won't work correctly if there are ids where the last id and next first id both have a status of 2. His proposed correction is shown below. Alternatively, the following (I think) will also work correctly: data have; input ID Status @@ ; cards; 1 1 1 2 1 2 1 2 2 1 2 2 2 2 3 1 3 1 3 1 4 1 4 2 5 2 5 1 ; run; data want; do until(last.id); set have; by id; case=ifn(first.id,0, ifn(Status*lag(Status) eq 4,1,case)); end; do until(last.id); set have; by id; if case ne 1 then case=0; output; end; run; HTH, Art p.s. Did you post an attempt at solving your latest request? If you did, I didn't see it. ------------- >hi ... Art's solution is fine unless there are two different IDs >with consecutive 2s in the data, for example ... > >data have; > input ID Status @@ ; > cards; >1 1 1 2 1 2 1 2 >2 1 2 2 2 2 >3 1 3 1 3 1 >4 1 4 2 >5 2 5 1 >; >run; > >data want; > do until(last.id); > set have; > by id; > if Status*lag(Status) eq 4 then case=1; > end; > > do until(last.id); > set have; > by id; > if case ne 1 then case=0; > output; > end; >run; > >proc print data=want; >run; > >OUTPUT ... ID = 5 should NOT be a CASE > >ID Status case > 1 1 1 > 1 2 1 > 1 2 1 > 1 2 1 > 2 1 1 > 2 2 1 > 2 2 1 > 3 1 0 > 3 1 0 > 3 1 0 > 4 1 0 > 4 2 0 > 5 2 1 > 5 1 1 > >so, another possibility ... > >data want; >length all $20; > do until(last.id); > set have; > by id; > all = catt(all,status); > end; > > do until(last.id); > set have; > by id; > case = (find(all,'22') gt 0); > output; > end; >drop all; >run; >-- >Mike Zdeb >U(a)Albany School of Public Health >One University Place (Room 119) >Rensselaer, New York 12144-3456 >P/518-402-6479 F/630-604-1475 > > Harshal, > > There are probably many ways to do what you want. One way, shown > below, uses what is known as a DOW loop to pass through the data > twice: once to test for your condition and the other to output the > records with the new field added: > > data have; > input ID Status; > cards; > 1 1 > 1 2 > 1 2 > 1 2 > 2 1 > 2 2 > 2 2 > 3 1 > 3 1 > 3 1 > ; > data want; > do until(last.id); > set have; > by id; > if Status*lag(Status) eq 4 then case=1; > end; > > do until(last.id); > set have; > by id; > if case ne 1 then case=0; > output; > end; > run; > > On Apr 24, 9:47 am, Harshal <harshaldeshm...(a)gmail.com> wrote: >> Dear All, >> >> I have a data which looks like this >> >> ID Status >> 1 1 >> 1 2 >> 1 2 >> 1 2 >> 2 1 >> 2 2 >> 2 2 >> 3 1 >> 3 1 >> 3 1 >> >> If any ID has two consecutive "2" as their status anywhere, then that >> person would be a case. I want a new variable which can give me the >> case=1 and controls(those who to not have two consecutive 2's)=0. >> >> Is there any simple way of doing this? >> >> I am new to programming and your help will be appreciated. >> >> regards, >> Harshal
|
Pages: 1 Prev: MACROS IN SAS. Next: Mean(x,y) vs (x+y)/2 |