Hi, I find the question a little unclear too, because the desired results don't seem to match the data in the rows you provided. Nonetheless, something like this might work: data a; input x $ 30.; cards; 28,16c2b8c4l8n 28,16cb8c4l8n 28,cbc4l24n ; run; data b (drop = position letter temp i); set a; original_record = _n_; do i = 1 to 10; /* guessing max here, can use other types of loops or get out of loop once temp is empty */ if i = 1 then temp = x; call missing(no_activity, contact, lunch, break, nonactivitiy); position = findc(temp,',clbn'); /* find location of , c l b n */ *put temp= position=; if position ne 0 then do; letter = substr(temp, position, 1); /* which letter is it? */ *put letter=; /* output 0 if no number before the letter */ /* should correct for automatic conversion */ if letter = ',' then do; if position = 1 then no_activity = 0; else no_activity = substr(temp, 1, position-1); output; end; else if letter = 'c' then do; if position = 1 then contact = 0; else contact = substr(temp, 1, position-1); output; end; else if letter = 'l' then do; if position = 1 then lunch = 0; else lunch = substr(temp, 1, position-1); output; end; else if letter = 'b' then do; if position = 1 then break = 0; else break = substr(temp, 1, position-1); output; end; else if letter = 'n' then do; if position = 1 then nonactivity = 0; else nonactivity = substr(temp, 1, position-1); output; end; temp = substr(temp, position+1); /* re-assign temp as the rest of the string */ end; end; run; proc sort data = b; by original_record; run; data c (keep = original_record total:) ; set b; by original_record; retain total_no_activity total_contact total_lunch total_break total_nonactivity ; if first.original_record then do; total_no_activity = 0; /* set to zero at the beginning of each group */ total_contact = 0; total_lunch = 0; total_break = 0; total_nonactivity = 0; end; if not missing(no_activity) then total_no_activity = total_no_activity + no_activity; /* add them up */ if not missing(contact) then total_contact = total_contact + contact; if not missing(lunch) then total_lunch = total_lunch + lunch; if not missing(break) then total_break = total_break + break; if not missing(nonactivity) then total_nonactivity = total_nonactivity + nonactivity; if last.original_record then output; /* only keep totals */ run; proc print data = c; run; Output: original_ total_no_ total_ total_ total_ total_ Obs record activity contact lunch break nonactivity 1 1 28 24 4 2 8 2 2 28 24 4 0 8 3 3 28 0 4 0 24
... View more