Hi @supp ,
I was wondering... If data are in a text file or as a datalines you could do it while reading.
All the best
Bart;
data have;
infile datalines truncover;
length owner pet_type pet_name $ 10;
keep owner pet_name;
input owner @;
input ;
i = 0;
do until(0);
i + 1;
pet_type = scan(_INFILE_,2*i);
if pet_type in (" " 'monkey') then
do;
pet_name = scan(_INFILE_,2*i+1); output; leave;
end;
end;
datalines;
kimberly dog sadie cat manson cat nellie monkey salty
landry dog manda
julie dog emmy dog sanchez
spencer cat armstrong dog cotton monkey jenkins monkey figgy monkey alex
penelope fish baldwin
welsh monkey constance dog mulder dog figgy monkey ac
alina cat chloe cat marls dog keen dog yasin cat avery cat harold monkey albert
roy cat eden
rachael fish gill
decker dog isabel monkey colossal
amira monkey curious
ramsey monkey george monkey cosmic dog jay cat kira cat ross cat kumar
;
run;
Bart, totally agree. However, if so, why monkey around with looping if _INFILE_ already has all we require without any need for concatenating? E.g.:
data want (keep = owner name) ;
informat owner name $10. ;
input owner ;
_x = findw (_infile_, "monkey") ;
if _x then name = scan (substr (_infile_, _x), 2) ;
cards ;
kimberly dog sadie cat manson cat nellie monkey salty
landry dog manda
julie dog emmy dog sanchez
spencer cat armstrong dog cotton monkey jenkins monkey figgy monkey alex
penelope fish baldwin
welsh monkey constance dog mulder dog figgy monkey ac
alina cat chloe cat marls dog keen dog yasin cat avery cat harold monkey albert
roy cat eden
rachael fish gill
decker dog isabel monkey colossal
amira monkey curious
ramsey monkey george monkey cosmic dog jay cat kira cat ross cat kumar
;
run ;
OTOH, looping can be done at a different angle by making use of the single trailing @ to read each next successive (pet_type pet_name) pair until pet_type="monkey" or the pairs are exhausted. E.g.:
data want (keep = owner name) ;
informat owner pet_type pet_name $10. ;
input owner @ ;
do _n_ = 1 to divide (countw (_infile_) - 1, 2) until (pet_type = "monkey") ;
input pet_type pet_name @ ;
end ;
if pet_type = "monkey" then name = pet_name ;
cards ;
kimberly dog sadie cat manson cat nellie monkey salty
landry dog manda
julie dog emmy dog sanchez
spencer cat armstrong dog cotton monkey jenkins monkey figgy monkey alex
penelope fish baldwin
welsh monkey constance dog mulder dog figgy monkey ac
alina cat chloe cat marls dog keen dog yasin cat avery cat harold monkey albert
roy cat eden
rachael fish gill
decker dog isabel monkey colossal
amira monkey curious
ramsey monkey george monkey cosmic dog jay cat kira cat ross cat kumar
;
run ;
Z poważaniem
Paul D.
Paul,
I couldn't agree more.
Bart
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.