From: sas analysis on 7 Apr 2010 17:12 Hi All, I have a multiple observation dataset. I would like to create variables with if case1 only, if case2 alone, if case1 and case2. The data looks as follows: Subject Case 1 Case2 1 0 0 1 0 1 1 0 0 1 1 0 1 0 0 2 0 0 2 0 1 2 0 0 3 1 0 3 0 0 3 0 0 3 0 0 and so on.... The code is the following: data want; data set; by ID; if case1=1 and case2=0 then case1new=1; else case1new=0; if case2=1 and case1=0 then case2new=1; else case2new=0; if case1=1 and case2=1 then case1and2=1; else case1and2=0; run; this is not giving me what I want. Here is what I want the output to give me: ID case1only case2only case1and2 1 0 0 1 2 0 1 0 3 1 0 0 any ideas would be great! Thanks!
From: Dave Haans on 7 Apr 2010 17:24 Try this (untested!): data temp; set cases; by id; if first.id then do; case1only=0; case2only=0; case1and2=0; case1temp=.; case2temp=.; end; /* Sum non-missing obs for case1 and case2 */ case1temp+case1; case2temp+case2; if last.id then do; /* e.g. if case1temp is not 0 or missing and case2temp is not missing or 0 then case1andcase2=1... */ if case1temp and not case2temp then case1only=1; if not case1temp and case2temp then case2only=1; if case1temp and case2temp then case1and2=1; output; end; run; Good luck! Cheers, Dave. On Apr 7, 5:12 pm, sas analysis <sasanaly...(a)gmail.com> wrote: > Hi All, > > I have a multiple observation dataset. I would like to create > variables with if case1 only, if case2 alone, if case1 and case2. > > The data looks as follows: > > Subject Case 1 Case2 > 1 0 0 > 1 0 1 > 1 0 0 > 1 1 0 > 1 0 0 > 2 0 0 > 2 0 1 > 2 0 0 > 3 1 0 > 3 0 0 > 3 0 0 > 3 0 0 > > and so on.... > > The code is the following: > data want; > data set; > by ID; > if case1=1 and case2=0 then case1new=1; > else case1new=0; > if case2=1 and case1=0 then case2new=1; > else case2new=0; > if case1=1 and case2=1 then case1and2=1; > else case1and2=0; > run; > > this is not giving me what I want. > > Here is what I want the output to give me: > > ID case1only case2only case1and2 > 1 0 0 1 > 2 0 1 0 > 3 1 0 0 > > any ideas would be great! > > Thanks!
From: Reeza on 7 Apr 2010 18:24 On Apr 7, 2:12 pm, sas analysis <sasanaly...(a)gmail.com> wrote: > Hi All, > > I have a multiple observation dataset. I would like to create > variables with if case1 only, if case2 alone, if case1 and case2. > > The data looks as follows: > > Subject Case 1 Case2 > 1 0 0 > 1 0 1 > 1 0 0 > 1 1 0 > 1 0 0 > 2 0 0 > 2 0 1 > 2 0 0 > 3 1 0 > 3 0 0 > 3 0 0 > 3 0 0 > > and so on.... > > The code is the following: > data want; > data set; > by ID; > if case1=1 and case2=0 then case1new=1; > else case1new=0; > if case2=1 and case1=0 then case2new=1; > else case2new=0; > if case1=1 and case2=1 then case1and2=1; > else case1and2=0; > run; > > this is not giving me what I want. > > Here is what I want the output to give me: > > ID case1only case2only case1and2 > 1 0 0 1 > 2 0 1 0 > 3 1 0 0 > > any ideas would be great! > > Thanks! Untested....but something like this should work... for each subject add the case1 for each subject down the rows using proc means. then use the if statements you have set up above on the resulting data set. proc means data=test noprint; by id; output out=sum(case1)=case1t sum(case2)=case2t; run; HTH, Reese
From: Patrick on 8 Apr 2010 05:49 Or this way (tested): data have; infile datalines truncover; input id case1 case2; datalines; 1 0 0 1 0 1 1 0 0 1 1 0 1 0 0 2 0 0 2 0 1 2 0 0 3 1 0 3 0 0 3 0 0 3 0 0 ; run; proc sql; /*create table want as*/ select id, case(sum(max(case1),max(case2))) when 2 then 1 else 0 end as case1and2, case(calculated case1and2) when 1 then 0 else max(case1) end as case1only, case(calculated case1and2) when 1 then 0 else max(case2) end as case2only from have group by id ; quit;
|
Pages: 1 Prev: Reading in BibTeX files using SAS Next: Apply your understanding of Accounts and Statistics |