Hello all, I am a complete newbie to SAS and mostly survived through this forum and UCLA thus far. I have rows of data on same subject (1 to 20+ occurrences). I have combined them using PROC means by class SubjectID, which worked well for continuous variables. However, I have a few categorical variables (e.g. WhichHand?, WhatTime?) for which I need one conditional observation per subject. For example, if a subject used only R hand=1 then newvar=1, only L=2 then newvar=2, but if they used R and L=2 OR both=3 then newvar=3. I tried to create a new variable using data step with BY statement, used first.subject last.subjectID tutorials, but nothing has worked. Instead of new variable doing a loop of all observation for subject, it basically copies the old variable. I am posting my most recent attempt, but I've basically given up and may just end up using mode or max in proc summary/means with By SubjectID at this point. PROC FORMAT;
VALUE TIMED
0 = 'morning'
1 = 'afternoon'
2 = 'evening'
3 = 'overnight';
VALUE HAND
1 = 'Left'
2 = 'Right'
3 = 'Both';
run;
DATA attemptWhichHand;
SET have;
BY subject_id;
IF ((WhichHand=3)) THEN HandUsed = 3;
else IF ((WhichHand=1) AND (WhichHand=2) AND (WhichHand=3)) THEN HandUsed = 3;
else IF ((WhichHand=1) AND (WhichHand=2)) THEN HandUsed = 3;
else IF ((WhichHand=1) AND (WhichHand=3)) THEN HandUsed = 3;
else IF ((WhichHand=2) AND (WhichHand=3)) THEN HandUsed = 3;
else IF (WhichHand=1) THEN HandUsed = 1;
else IF ((WhichHand=2)) THEN HandUsed = 2;
FORMAT HandUsed HAND.;
run;
/* This is what I did with other variables*/
proc means noprint data=HM_Meta.glasssheet NWAY ;
class SUBJECT_ID ;
id DOB;
output out=hand (drop=_type_ rename=(_freq_=TotalCollect))
Min (Age) = MinAgeWeek
Max (Age) = MaxAgeWeek
Range (Age) = RAgeWeek
Min (DATE) = MinDate
Max (DATE) = MaxDate
Max (Time WhichHand) = TimeMx WhichHandMx
/*Find a better way to summarize these variables - create new varibale only left, only right, both*/
run;/*925*/
proc print data=attempt (obs=50);
run; SubjectID date WhichHand? Time AgeMilk DOB .....
1 24Jul2020 1 0
1 20Jul2020 2 2
1 29Jul2020 1 3
2 28Jul2020 3 0
3 30Jul2020 1 4 Sorry about the poor quality of post and if this has been answered and thank you for your time 🙂
... View more