BookmarkSubscribeRSS Feed
supp
Pyrite | Level 9
@hasman, thanks for catching and fixing the input specs. I noticed the names getting truncated and now I know why!
yabwon
Amethyst | Level 16

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;
_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



hashman
Ammonite | Level 13

@yabwon:

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. 

yabwon
Amethyst | Level 16

Paul,

 

I couldn't agree more.

 

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



supp
Pyrite | Level 9
Very nice, thanks for the clever solutions!

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

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!

Register Now

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 19 replies
  • 3479 views
  • 15 likes
  • 8 in conversation