If searching for 'ice' rather than 'ice cream' would suffice, then here is one method:
data have (keep=id j scoops flavor);
informat comments $255.;
length scoops flavor $10;
input id comments &;
i=0;
j=0;
ixp=0;
ixs=-2;
do while (findw(comments,"ice",' ,','e',ixs+3));
i+1;
if i eq 1 then ixs+3;
ixp=findw(comments,"ice",' ,','e',ixs)+ixp;
if (scan(comments,ixp-3,' ') eq 'scoops' or
scan(comments,ixp-3,' ') eq 'scoop') and
scan(comments,ixp-2,' ') eq 'of' then do;
j+1;
scoops=catx(' ',scan(comments,ixp-4,' '),scan(comments,ixp-3,' '));
flavor = scan(comments,ixp-1,' ');
output;
end;
ixs=findw(comments,"ice",ixs)+3;
end;
cards;
1 On Monday I had 1.5 scoops of vanilla ice cream, Tuesday we had 2 scoops of chocolate ice cream, one day we also had 3 scoops of strawberry ice cream, but I can't remember when.
2 Over the week I had 4 scoops of raspberry ice cream, 3 scoops of vanilla ice cream
3 On 1/20/2019, I had 3 scoops of chocolate ice cream.
4 On 2/1/2019 I didn't want any ice cream
5 On 2/1/2019 I didn't want any ice cream, but the day before I had 1 scoop of lemon ice
;
%transpose(data=have,out=want, var=scoops flavor, id=j, by=id)
/*
uses transpose macro which can be downloaded from
http://www.sascommunity.org/wiki/A_Better_Way_to_Flip_(Transpose)_a_SAS_Dataset
*/
Art, CEO, AnalystFinder.com
... View more