<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Find the name of the owner's favorite monkey in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Find-the-name-of-the-owner-s-favorite-monkey/m-p/590512#M169019</link>
    <description>&lt;P&gt;Ha! Sorry for having to make you say it .7 times more. Maybe this should be added to &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;'s&amp;nbsp;&lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/Maxims-of-Maximally-Efficient-SAS-Programmers/ta-p/352068" target="_self"&gt;Maxims of Maximally Efficient SAS Programmers&lt;/A&gt;&amp;nbsp;!?&lt;/P&gt;</description>
    <pubDate>Fri, 20 Sep 2019 18:05:41 GMT</pubDate>
    <dc:creator>supp</dc:creator>
    <dc:date>2019-09-20T18:05:41Z</dc:date>
    <item>
      <title>Find the name of the owner's favorite monkey</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-the-name-of-the-owner-s-favorite-monkey/m-p/590388#M168964</link>
      <description>&lt;P&gt;If we have some data on pets and their owners such as:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	infile datalines truncover;
		input owner $ pet_type1 $ pet_name_1 $ pet_type2 $ pet_name_2 $
			      pet_type3 $ pet_name_3 $ pet_type4 $ pet_name_4 $
			      pet_type5 $ pet_name_5 $ pet_type6 $ pet_name_6 $
			      pet_type7 $ pet_name_7 $ ;
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;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;One owner can have up to 7 pets, all listed on one row. The pets have been ranked in order of the owners favorite. The structure is pet_type1, pet_name1 (most favorite) to pet_type7, pet_name7 (least favorite). The desired output is listing all owners in the original data with just the name of their favorite monkey. If they have multiple monkeys (pet # 1 and pet #3), we want to return the name from the lowest pet #.&amp;nbsp; In the example data we would want to return:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;kimberly salty
landry 
julie
spencer jenkins
penelope
welsh constance
alina albert
roy
rachael
decker colossal
amira curious
ramsey george&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I am sure I can do this with a bunch of if statements. Is there a better approach to this? Maybe there is already a function that can handle this?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks in advance!&lt;/P&gt;</description>
      <pubDate>Fri, 20 Sep 2019 13:29:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-the-name-of-the-owner-s-favorite-monkey/m-p/590388#M168964</guid>
      <dc:creator>supp</dc:creator>
      <dc:date>2019-09-20T13:29:52Z</dc:date>
    </item>
    <item>
      <title>Re: Find the name of the owner's favorite monkey</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-the-name-of-the-owner-s-favorite-monkey/m-p/590396#M168966</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
    set have;
    keep owner fav_monkey;
    array pets pet:;
    fst_monkey_idx=whichc("monkey", of pets(*));
    if fst_monkey_idx then fav_monkey=pets(fst_monkey_idx+1);
run;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 20 Sep 2019 13:40:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-the-name-of-the-owner-s-favorite-monkey/m-p/590396#M168966</guid>
      <dc:creator>gamotte</dc:creator>
      <dc:date>2019-09-20T13:40:40Z</dc:date>
    </item>
    <item>
      <title>Re: Find the name of the owner's favorite monkey</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-the-name-of-the-owner-s-favorite-monkey/m-p/590397#M168967</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data want;
    set have;
    length favorite_monkey $ 16;
    array pet_type pet_type:;
    array pet_name pet_name:;
    do i=1 to dim(pet_type);
        if pet_type(i)='monkey' then do;
            favorite_monkey=pet_name(i);
            leave;
        end;
    end;
    keep owner favorite_monkey;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 20 Sep 2019 13:41:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-the-name-of-the-owner-s-favorite-monkey/m-p/590397#M168967</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-09-20T13:41:10Z</dc:date>
    </item>
    <item>
      <title>Re: Find the name of the owner's favorite monkey</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-the-name-of-the-owner-s-favorite-monkey/m-p/590400#M168969</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want(keep=owner Favoritemonkey);
    set have;
    array p{*} $ pet_type1--pet_name_7;
    idx=whichc("monkey", of p(*));
    Favoritemonkey=ifc(idx=0, '', p[idx+1]);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 20 Sep 2019 13:45:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-the-name-of-the-owner-s-favorite-monkey/m-p/590400#M168969</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-09-20T13:45:18Z</dc:date>
    </item>
    <item>
      <title>Re: Find the name of the owner's favorite monkey</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-the-name-of-the-owner-s-favorite-monkey/m-p/590403#M168971</link>
      <description>thanks &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31304"&gt;@PeterClemmensen&lt;/a&gt;. Can you explain the double dash '--'  for declaring pet_type as array elements? Could we just use a single dash? Is there a difference?</description>
      <pubDate>Fri, 20 Sep 2019 13:51:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-the-name-of-the-owner-s-favorite-monkey/m-p/590403#M168971</guid>
      <dc:creator>supp</dc:creator>
      <dc:date>2019-09-20T13:51:49Z</dc:date>
    </item>
    <item>
      <title>Re: Find the name of the owner's favorite monkey</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-the-name-of-the-owner-s-favorite-monkey/m-p/590404#M168972</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18331"&gt;@supp&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;thanks &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31304"&gt;@PeterClemmensen&lt;/a&gt;. Can you explain the double dash '--' for declaring pet_type as array elements? Could we just use a single dash? Is there a difference?&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;The code is not declaring pet-type as an array. It is declaring pet_type and pet_name into the same array.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The double-dash is used because it takes all variables from pet_&lt;FONT color="#FF0000"&gt;type&lt;/FONT&gt;1 through pet_&lt;FONT color="#FF0000"&gt;name&lt;/FONT&gt;7 and includes all of those in the array.&lt;/P&gt;</description>
      <pubDate>Fri, 20 Sep 2019 13:56:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-the-name-of-the-owner-s-favorite-monkey/m-p/590404#M168972</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-09-20T13:56:26Z</dc:date>
    </item>
    <item>
      <title>Re: Find the name of the owner's favorite monkey</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-the-name-of-the-owner-s-favorite-monkey/m-p/590405#M168973</link>
      <description>&lt;P&gt;I haven't validated it for all scenarios, but this works for this specific example. You will have to test it on others.&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;The key is using a loop and arrays to restructure the data, then begin testing for a monkey. First time you see a monkey end the restructure loop and output the row. If I run the entire loop and don't find a monkey then just output the last row which is the owners name but no value for pet.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
	set have;
	array Pet_Type[*] pet_type:; /*Set values of all pet types in an array*/
        array Pet_Name[*] pet_name:; /*Set values of all pet names in an array*/
	FavoriteNum=0; /*Start a numbered list for favorite value of pet. Don't need this, just used it to see*/
	do i=1 to dim(Pet_Type); /*Run a loop and restructure the data based on the number of values in the array. Then test if it's a monkey. If it's a monkey output the row and end the loop since it's the first*/
		FavoriteNum+1; /*Increase the favorite overall number of the pet*/
		PetName=Pet_Name[i];
		PetType=Pet_Type[i];
		if upcase(PetType)="MONKEY" then do;
			output;
			leave; /*Leave loop*/
		end;
		else if dim(Pet_Type)=FavoriteNum and PetType ne "MONKEY" then output; /*If it makes it to the last row of the owner and no monkey is found, then output the row with nothing*/
 	end;
	drop i pet_:;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 20 Sep 2019 13:57:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-the-name-of-the-owner-s-favorite-monkey/m-p/590405#M168973</guid>
      <dc:creator>Panagiotis</dc:creator>
      <dc:date>2019-09-20T13:57:39Z</dc:date>
    </item>
    <item>
      <title>Re: Find the name of the owner's favorite monkey</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-the-name-of-the-owner-s-favorite-monkey/m-p/590406#M168974</link>
      <description>&lt;P&gt;Sir, The linear method of yours interesting runs a lot faster than &lt;EM&gt;whichc&lt;/EM&gt;(of course not taking away a fraction of full credit to&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/30622"&gt;@gamotte&lt;/a&gt;&amp;nbsp;idea) on my citizens machine for some reason. I have no clue why. I would have thought &lt;EM&gt;whichc&lt;/EM&gt; to be faster. Or is it just my machine? Any thoughts?&lt;/P&gt;</description>
      <pubDate>Fri, 20 Sep 2019 13:59:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-the-name-of-the-owner-s-favorite-monkey/m-p/590406#M168974</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-09-20T13:59:04Z</dc:date>
    </item>
    <item>
      <title>Re: Find the name of the owner's favorite monkey</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-the-name-of-the-owner-s-favorite-monkey/m-p/590410#M168975</link>
      <description>Thanks for the clarification! I missed that it was including both type and name into the array. I need to slow down.</description>
      <pubDate>Fri, 20 Sep 2019 14:02:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-the-name-of-the-owner-s-favorite-monkey/m-p/590410#M168975</guid>
      <dc:creator>supp</dc:creator>
      <dc:date>2019-09-20T14:02:17Z</dc:date>
    </item>
    <item>
      <title>Re: Find the name of the owner's favorite monkey</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-the-name-of-the-owner-s-favorite-monkey/m-p/590438#M168982</link>
      <description>&lt;P&gt;I don't know if this accounts for much of the speed difference, but WHICHC checks both pet_type and pet_name for monkey, whereas my two array method only checks pet_type for monkey.&lt;/P&gt;</description>
      <pubDate>Fri, 20 Sep 2019 15:16:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-the-name-of-the-owner-s-favorite-monkey/m-p/590438#M168982</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-09-20T15:16:40Z</dc:date>
    </item>
    <item>
      <title>Re: Find the name of the owner's favorite monkey</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-the-name-of-the-owner-s-favorite-monkey/m-p/590450#M168985</link>
      <description>&lt;P&gt;I applied a couple of these solutions to the real world scenario. The data is similar to what I posted and a little over 700,000 rows. If we call&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/30622"&gt;@gamotte&lt;/a&gt;'s approach as approach #1 and&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;'s approach as approach #2.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Where approach #1 sets all variables into a large array and uses whichc function to find and return the array element and value of interest.&lt;/P&gt;
&lt;P&gt;and&lt;/P&gt;
&lt;P&gt;Where approach # 2 defines multiple smaller arrays and a do loop to find element of interest in one array and return value of interest from second array.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Both solutions are working and returning the same results (at least at first glance). Both solutions are extremely fast and for practical purposes there is no real difference in performance.&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;'s approach does seem to be slightly faster on average. I am getting user CPU times around .2 seconds.&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/30622"&gt;@gamotte&lt;/a&gt;&amp;nbsp;'s solution is getting around .3 seconds user CPU time. This doesn't seem to be significant enough to matter for a user.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think I like using the smaller, more defined array's in&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;'s approach as I find it easier to visualize what is happening in the code. If we add another type of variable to the data, like pet_breed, it would increase the number of elements into one large array. In the real world scenario I started getting confused with which variable was which element in the array. Any thoughts or feedback on this would be appreciated.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Along these lines is there a way to show or display what variable is what array element? I found this code &lt;I&gt;"put (pets[*]) (=/);"&amp;nbsp;&lt;/I&gt;Which was helpful, is there anything more explicit that would show array element 1 = variable name?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 20 Sep 2019 15:56:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-the-name-of-the-owner-s-favorite-monkey/m-p/590450#M168985</guid>
      <dc:creator>supp</dc:creator>
      <dc:date>2019-09-20T15:56:55Z</dc:date>
    </item>
    <item>
      <title>Re: Find the name of the owner's favorite monkey</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-the-name-of-the-owner-s-favorite-monkey/m-p/590458#M168988</link>
      <description>&lt;P&gt;I applied a couple of these solutions to the real world scenario. The data is similar to what I posted and a little over 700,000 rows. If we call&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/30622"&gt;@gamotte&lt;/a&gt;'s approach as approach #1 and&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;'s approach as approach #2.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Where approach #1 sets all variables into a large array and uses whichc function to find and return the array element and value of interest.&lt;/P&gt;
&lt;P&gt;and&lt;/P&gt;
&lt;P&gt;Where approach # 2 defines multiple smaller arrays and a do loop to find element of interest in one array and return value of interest from second array.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Both solutions are working and returning the same results (at least at first glance). Both solutions are extremely fast and for practical purposes there is no real difference in performance. PaigeMiller's&amp;nbsp;approach does seem to be slightly faster on average. I am getting user CPU times around .2 seconds. gamotte's&amp;nbsp;solution is getting around .3 seconds user CPU time. This doesn't seem to be significant enough to matter for a user.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think I like using the smaller, more defined array's in&amp;nbsp;PaigeMiller&amp;nbsp;'s approach as I find it easier to visualize what is happening in the code. If we add another type of variable to the data, like pet_breed, it would increase the number of elements into one large array. In the real world scenario I started getting confused with which variable was which element in the array. Any thoughts or feedback on this would be appreciated.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Along these lines is there a way to show or display what variable is what array element? I found this code &lt;I&gt;"put (pets[*]) (=/);"&amp;nbsp;&lt;/I&gt;Which was helpful, is there anything more explicit that would show array element 1 = variable name?&lt;/P&gt;</description>
      <pubDate>Fri, 20 Sep 2019 15:58:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-the-name-of-the-owner-s-favorite-monkey/m-p/590458#M168988</guid>
      <dc:creator>supp</dc:creator>
      <dc:date>2019-09-20T15:58:53Z</dc:date>
    </item>
    <item>
      <title>Re: Find the name of the owner's favorite monkey</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-the-name-of-the-owner-s-favorite-monkey/m-p/590505#M169014</link>
      <description>&lt;P&gt;If I have said it once, I have probably said it at least 3.7 times now&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Don't use WHICHC to find monkeys.&lt;/P&gt;</description>
      <pubDate>Fri, 20 Sep 2019 17:58:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-the-name-of-the-owner-s-favorite-monkey/m-p/590505#M169014</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-09-20T17:58:04Z</dc:date>
    </item>
    <item>
      <title>Re: Find the name of the owner's favorite monkey</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-the-name-of-the-owner-s-favorite-monkey/m-p/590512#M169019</link>
      <description>&lt;P&gt;Ha! Sorry for having to make you say it .7 times more. Maybe this should be added to &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;'s&amp;nbsp;&lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/Maxims-of-Maximally-Efficient-SAS-Programmers/ta-p/352068" target="_self"&gt;Maxims of Maximally Efficient SAS Programmers&lt;/A&gt;&amp;nbsp;!?&lt;/P&gt;</description>
      <pubDate>Fri, 20 Sep 2019 18:05:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-the-name-of-the-owner-s-favorite-monkey/m-p/590512#M169019</guid>
      <dc:creator>supp</dc:creator>
      <dc:date>2019-09-20T18:05:41Z</dc:date>
    </item>
    <item>
      <title>Re: Find the name of the owner's favorite monkey</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-the-name-of-the-owner-s-favorite-monkey/m-p/590569#M169042</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18331"&gt;@supp&lt;/a&gt;:&lt;/P&gt;
&lt;P&gt;You input spec $ defaults to $8 and is insufficient to accommodate names like "armstrong" or "constance", which thus get truncated. Hence, below I made it $10 and sized the concatenated variable _C accordingly, plus likewise made the length of output variable NAME $10.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;                                                                    
  infile cards truncover ;                                                     
  input (owner                                                                 
         pet_type1 pet_name_1                                                  
         pet_type2 pet_name_2                                                  
         pet_type3 pet_name_3                                                  
         pet_type4 pet_name_4                                                  
         pet_type5 pet_name_5                                                  
         pet_type6 pet_name_6                                                  
         pet_type7 pet_name_7)                                                 
        (:$10.) ;                                                              
  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 ;                                                                          
                                                                               
data want (keep = owner name) ;                                                
  set have ;                                                                   
  _c = put (catx (" ", of pet_:), $76.) ;                                      
  _x = findw (_c, "monkey") ;                                                  
  if _x then name = put (scan (substr (_c, _x), 2), $10.) ;                    
run ;                                                                          
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If it were a competition for the least number of program statements, _C and _X could be plugged into the IF statement:&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (keep = owner name) ;                                                                                                         
  set have ;                                                                                                                            
  if findw (catx (" ", of pet_:), "monkey") then name = put (scan (substr (catx (" ", of pet_:), findw (catx (" ", of pet_:), "monkey") 
), 2), $10.) ;                                                                                                                          
run ; 
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But for anything other than this fictitious "competition", it would make no sense from the standpoints of clarity and unnecessarily repeated identical function calls.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 21 Sep 2019 05:00:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-the-name-of-the-owner-s-favorite-monkey/m-p/590569#M169042</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-09-21T05:00:00Z</dc:date>
    </item>
    <item>
      <title>Re: Find the name of the owner's favorite monkey</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-the-name-of-the-owner-s-favorite-monkey/m-p/590588#M169050</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18331"&gt;@supp&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I was wondering... If data are in a text file or as a datalines you could do it while reading.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;All the best&lt;/P&gt;&lt;P&gt;Bart;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 21 Sep 2019 12:05:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-the-name-of-the-owner-s-favorite-monkey/m-p/590588#M169050</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2019-09-21T12:05:43Z</dc:date>
    </item>
    <item>
      <title>Re: Find the name of the owner's favorite monkey</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-the-name-of-the-owner-s-favorite-monkey/m-p/590637#M169085</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/35763"&gt;@yabwon&lt;/a&gt;:&lt;/P&gt;
&lt;P&gt;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.:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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 ;                      
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;OTOH, looping can be done at a different angle by making use of the single trailing&amp;nbsp;@ to read each next successive (pet_type pet_name) pair until pet_type="monkey" or the pairs are exhausted. E.g.:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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 ;                                                                           
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Z poważaniem&lt;/P&gt;
&lt;P&gt;Paul D.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 21 Sep 2019 18:12:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-the-name-of-the-owner-s-favorite-monkey/m-p/590637#M169085</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-09-21T18:12:04Z</dc:date>
    </item>
    <item>
      <title>Re: Find the name of the owner's favorite monkey</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-the-name-of-the-owner-s-favorite-monkey/m-p/590639#M169087</link>
      <description>&lt;P&gt;Paul,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I couldn't agree more.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Sat, 21 Sep 2019 18:34:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-the-name-of-the-owner-s-favorite-monkey/m-p/590639#M169087</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2019-09-21T18:34:26Z</dc:date>
    </item>
    <item>
      <title>Re: Find the name of the owner's favorite monkey</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-the-name-of-the-owner-s-favorite-monkey/m-p/590886#M169181</link>
      <description>@hasman, thanks for catching and fixing the input specs. I noticed the names getting truncated and now I know why!</description>
      <pubDate>Mon, 23 Sep 2019 12:33:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-the-name-of-the-owner-s-favorite-monkey/m-p/590886#M169181</guid>
      <dc:creator>supp</dc:creator>
      <dc:date>2019-09-23T12:33:47Z</dc:date>
    </item>
    <item>
      <title>Re: Find the name of the owner's favorite monkey</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-the-name-of-the-owner-s-favorite-monkey/m-p/590887#M169182</link>
      <description>Very nice, thanks for the clever solutions!</description>
      <pubDate>Mon, 23 Sep 2019 12:36:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-the-name-of-the-owner-s-favorite-monkey/m-p/590887#M169182</guid>
      <dc:creator>supp</dc:creator>
      <dc:date>2019-09-23T12:36:42Z</dc:date>
    </item>
  </channel>
</rss>

