<?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: How to create columns with values based on occurence of words (from predefined lists) in a strin in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-columns-with-values-based-on-occurence-of-words/m-p/599344#M172999</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/21262"&gt;@hashman&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hi, I discovered 2 more things that aren't working as I expected:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;When the VARIABLES table contains mulitple-word values, but the partial values are not in the value list (e.g. "South America" is there, but "South" by itself is not) they are also searched for in the STRINGS and if they are found, they are output in the new column (see the column regions in obs #2 and #8). I'd like to stop this from happening adn only search for the whole values if they have multiple-words.&lt;/LI&gt;&lt;LI&gt;In VARIABLES table it happens that the same value is in multiple columns (e.g. beans, carrot... - I added a new column with the general type of vegetable). When this occurs, the found value is output only in one variable column it was found in, it doesn't repeat in others (see columns vegetable and vegetablegeneral in obs #1,2,8,9). I'd like for these values to repeat in all the output column were they should be base don the original VARIABLES columns.&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;My output:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="image.png" style="width: 571px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/33400i35C99BE897081B58/image-size/large?v=v2&amp;amp;px=999" role="button" title="image.png" alt="image.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Desired output:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="image.png" style="width: 569px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/33399i8C312B4E7CE75E85/image-size/large?v=v2&amp;amp;px=999" role="button" title="image.png" alt="image.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please, do you have any tip on how to modify the code to achieve this?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The code so far with new data:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data strings ;                                                                                                                          
  input string $30.;                                                                                                                    
  cards ;                                                                                                                               
Ford carrot                                                                                                                             
Honda beans South Africa                                                                                                                             
south america black beans                                                                                                               
green beans Audi                                                                                                                        
x5 chili pepper                                                                                                   
garlic north America                                                                                                                    
central europe bmw                                                                                                                      
lettuce north onion X5                                                                                                                        
America pepper                                                                                                                     
X5 BMW Europe                                                                                                                           
Central East Europe                                                                                                                     
bmw X5 america                                                                                                                          
;                                                                                                                                       
run ;                                                                                                                                   
                                                                                                                                        
data variables;                                                                                                                         
   infile datalines delimiter=',' dsd;                                                                                                  
   length cars $6 vegetablegeneral $7 vegetable $12 regions $14;                                                                                           
   input cars $ vegetablegeneral $ vegetable $ regions $;                                                                                                 
   datalines;                                                                                                                           
Audi,beans,beans,America                                                                                                                      
BMW,carrot,black beans,Central Europe                                                                                                          
BMW X5,lettuce,carrot,East Europe                                                                                                               
X5,onion,chili pepper,Europe                                                                                                                  
Honda,pepper,green beans,North America                                                                                                         
,,lettuce,South America                                                                                                                  
,,onion,                                                                                                                                 
,,pepper,                                                                                                                                
;                                                                                                                                       
run;                                                                                                                                    
                                                                                                                                        
proc sql noprint ;                                                                                                                      
  select max (length) into :maxvlen from dictionary.columns                                                                             
  where libname="WORK" and memname="VARIABLES"                                                                                          
  ;                                                                                                                                     
quit ;      

proc sql noprint;                                                                                                                      
  select name into :varlist separated by ' ' from dictionary.columns
  where libname="WORK" and memname="VARIABLES";
quit;   
                                                                                                                                        
data want (drop = _:) ;                                                                                                                 
  set strings ;                                                                                                                         
  if _n_ = 1 then do ;                                                                                                                  
    length _s _z $ &amp;amp;maxvlen ;                                                                                                           
    dcl hash h () ;                                                                                                                     
    h.definekey ("_s") ;                                                                                                                
    h.definedata ("_z", "_i_") ;                                                                                                        
    h.definedone () ;                                                                                                                   
    do until (z) ;                                                                                                                      
      set variables end = z ;                                                                                                           
      array v &amp;amp;varlist ;                                                                                                 
      do over v ;                                                                                                                       
        if cmiss (v) then continue ;                                                                                                    
        do _i = 1 to countw (v) ;                                                                                                       
          _z = scan (v, _i) ;                                                                                                           
          h.ref (key: upcase (_z), data: _z, data: _i_) ;                                                                               
          do _j = _i + 1 to countw (v) ;                                                                                                
            _z = catx (" ", _z, scan (v, _j)) ;                                                                                         
            h.ref (key: upcase (_z), data: _z, data: _i_) ;                                                                             
          end ;                                                                                                                         
        end ;                                                                                                                           
      end ;                                                                                                                             
    end ;                                                                                                                               
  end ;                                                                                                                                 
  call missing (of v[*]) ;                                                                                                              
  do _i = 1 to countw (string) ;                                                                                                        
    _s = scan (string, _i) ;                                                                                                            
    if h.find (key: upcase (_s)) = 0 then if length (_z) &amp;gt; length (v) then v = _z ;                                                     
    do _j = _i + 1 to countw (string) ;                                                                                                 
      _s = catx (" ", _s, scan (string, _j)) ;                                                                                          
      if h.find (key: upcase (_s)) = 0 then if length (_z) &amp;gt; length (v) then v = _z ;                                                   
    end ;                                                                                                                               
  end ;                                                                                                                                 
run ;    &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 25 Oct 2019 14:26:37 GMT</pubDate>
    <dc:creator>LuciaCekanakova</dc:creator>
    <dc:date>2019-10-25T14:26:37Z</dc:date>
    <item>
      <title>How to create columns with values based on occurence of words (from predefined lists) in a string?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-columns-with-values-based-on-occurence-of-words/m-p/595563#M171359</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;I'd like your help with this problem.&lt;/P&gt;&lt;P&gt;I have 2 tables:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="image.png" style="width: 134px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/33053i6B596370D7F1E544/image-size/large?v=v2&amp;amp;px=999" role="button" title="image.png" alt="image.png" /&gt;&lt;/span&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="image.png" style="width: 153px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/33054i272304549E1DCE82/image-size/large?v=v2&amp;amp;px=999" role="button" title="image.png" alt="image.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to take each string in the STRINGS table, go over the list of values of each variable in VARIABLES table and if the value from this list occurs in the string, the value should be written into a new column that has the same name as the variable.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;See the output table for better understanding:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="image.png" style="width: 162px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/33056iF36136D2D56EA94B/image-size/large?v=v2&amp;amp;px=999" role="button" title="image.png" alt="image.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The number of columns in VARIABLES table may vary, and the column names won't always be as regular as var1, var2...&lt;/P&gt;&lt;P&gt;Also, if the string contains more than 1 value fromt he list (e.g. the last observation in STRINGS table), it has to have assigned only 1 value in the new column, no matter which one (so in case of string "a b", it could be assigned either "a", "b" or "a b"). Important thing is for at least one value to be there without duplication of the original string in another row.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am a beginner in coding in SAS, so the only way I could do this is by typing out a zillion of IF conditions for all the values for creating each new column. However, I'd like the solution to be automatic so that it doesn't matter how many variables there are, what are their names and how many values each one has.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please, can anyone help me with this?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here's the code for creating my data:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data strings; 
   input string $6.;
   datalines;                      
a x
aa l z
b k
b y m
c z
c x k
a b
;

data variables;
   infile datalines delimiter=',' dsd; 
   input var1 $ var2 $ var3 $;
   datalines;                      
a,k,x
b,l,y
a b,m,z
,n,
;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Thu, 10 Oct 2019 20:42:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-columns-with-values-based-on-occurence-of-words/m-p/595563#M171359</guid>
      <dc:creator>LuciaCekanakova</dc:creator>
      <dc:date>2019-10-10T20:42:57Z</dc:date>
    </item>
    <item>
      <title>Re: How to create columns with values based on occurence of words (from predefined lists) in a strin</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-columns-with-values-based-on-occurence-of-words/m-p/595568#M171364</link>
      <description>&lt;P&gt;Up until your value of 'a b' this looked moderately routine. However since you are looking for a single value of 'a' from the first line of your Strings data it is going to be a bit difficult to search for just the 'a' except when 'a b' is present in a generic sense.&lt;/P&gt;
&lt;P&gt;I ask for clarity sake, does your actual working data have exactly 1 or many Variables values that include other variable values as substrings? If so then you may need to provide more information or a more complex example closer to your actual data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Best might be to find some way to split up that String variable into the component parts that you need if possible so that you have values of 'a' alone instead of 'a x'.&lt;/P&gt;</description>
      <pubDate>Thu, 10 Oct 2019 21:20:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-columns-with-values-based-on-occurence-of-words/m-p/595568#M171364</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-10-10T21:20:51Z</dc:date>
    </item>
    <item>
      <title>Re: How to create columns with values based on occurence of words (from predefined lists) in a strin</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-columns-with-values-based-on-occurence-of-words/m-p/595620#M171391</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/190130"&gt;@LuciaCekanakova&lt;/a&gt;:&lt;/P&gt;
&lt;P&gt;You can store VARIABLES in a lookup table and then, in principle, go two ways:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Go through the table sequentially for every record from STRINGS and use the FIND function to look into STRING for every value in the table. But with this approach, you'll have to iterate through the whole table for every record in STRINGS in O(N) time.&lt;/LI&gt;
&lt;LI&gt;Parse STRING into all possible left-to-right substrings and search for each of them in the lookup table in O(1) time.&amp;nbsp;&amp;nbsp;&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;The first approach is simpler since you don't have to write code to parse STRING, and yet I still find it the other one better because it's likely to yield much better performance; so this is what is adopted in the code below.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data strings ;                                                                                                                          
  input string $6. ;                                                                                                                    
  cards ;                                                                                                                               
a x                                                                                                                                     
aa l z                                                                                                                                  
b k                                                                                                                                     
b y m                                                                                                                                   
c z                                                                                                                                     
c x k                                                                                                                                   
a b                                                                                                                                     
;                                                                                                                                       
run ;                                                                                                                                   
                                                                                                                                        
data variables ;                                                                                                                        
  input (var1-var3) (&amp;amp; :$3.) ;                                                                                                          
  cards ;                                                                                                                               
a    k  x                                                                                                                               
b    l  y                                                                                                                               
a b  m  z                                                                                                                               
.    n  .                                                                                                                               
;                                                                                                                                       
run ;                                                                                                                                   
                                                                                                                                        
data want (keep = string var:) ;                                                                                                        
  set strings ;                                                                                                                         
  if _n_ = 1 then do ;                                                                                                                  
    dcl hash h () ;                                                                                                                     
    h.definekey ("_s") ;                                                                                                                
    h.definedata ("_i_") ;                                                                                                              
    h.definedone () ;                                                                                                                   
    do until (z) ;                                                                                                                      
      set variables end = z ;                                                                                                           
      array v var: ;                                                                                                                    
      do over v ;                                                                                                                       
        if cmiss (v) = 0 then h.add (key:v, data:_i_) ;                                                                                 
      end ;                                                                                                                             
    end ;                                                                                                                               
  end ;                                                                                                                                 
  _s = string ;                                                                                                                         
  call missing (_s, of var:) ;                                                                                                          
  do _i = 1 to countw (string) ;                                                                                                        
    _s = scan (string, _i) ;                                                                                                            
    if h.find (key: _s) = 0 then v = _s ;                                                                                               
    do _j = _i + 1 to countw (string) ;                                                                                                 
      _s = catx (" ", _s, scan (string, _j)) ;                                                                                          
      if h.find (key: _s) = 0 then v = _s ;                                                                                             
    end ;                                                                                                                               
  end ;                                                                                                                                 
run ;                                                   
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note that, as&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;has mentioned, the fact that any of VAR1-VAR3 can contain more than one component (in your case,&amp;nbsp; "a b" in VAR1) throws a kind of monkey wrench into the task: Without it, parsing STRING would be a simple scan, rather than the nested scan requiring the concatenation to account for all possible left-to-right substrings without losing any possible matches with VARIABLES. But specs are specs.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&lt;/P&gt;</description>
      <pubDate>Fri, 11 Oct 2019 03:25:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-columns-with-values-based-on-occurence-of-words/m-p/595620#M171391</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-10-11T03:25:43Z</dc:date>
    </item>
    <item>
      <title>Re: How to create columns with values based on occurence of words (from predefined lists) in a strin</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-columns-with-values-based-on-occurence-of-words/m-p/595623#M171392</link>
      <description>&lt;P&gt;I am unsure what the actual data looks like, but you are likely to have multiple matches.&lt;/P&gt;
&lt;P&gt;You may want to split the logic into two steps:&lt;/P&gt;
&lt;P&gt;1. Match the data&lt;/P&gt;
&lt;P&gt;2. Decide on the matches to keep.&lt;/P&gt;
&lt;P&gt;This does the first step:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  create table WANT as
  select STRING
        ,ifc(findw(STRING,VAR1,' ','r'), VAR1,' ') as VAR1 length=8
        ,ifc(findw(STRING,VAR2,' ','r'), VAR2,' ') as VAR2 length=8
        ,ifc(findw(STRING,VAR3,' ','r'), VAR3,' ') as VAR3 length=8
   from STRINGS,VARIABLES
   having lengthn(VAR1) | lengthn(VAR2) | lengthn(VAR3)
  order by STRING;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE style="border-collapse: collapse; width: 192pt;" border="0" width="256" cellspacing="0" cellpadding="0"&gt;
&lt;TBODY&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD width="64" height="20" style="height: 15.0pt; width: 48pt;"&gt;STRING&lt;/TD&gt;
&lt;TD width="64" style="width: 48pt;"&gt;VAR1&lt;/TD&gt;
&lt;TD width="64" style="width: 48pt;"&gt;VAR2&lt;/TD&gt;
&lt;TD width="64" style="width: 48pt;"&gt;VAR3&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" style="height: 15.0pt;"&gt;a x&lt;/TD&gt;
&lt;TD&gt;a&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;x&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" style="height: 15.0pt;"&gt;aa l z&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;l&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" style="height: 15.0pt;"&gt;aa l z&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;z&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" style="height: 15.0pt;"&gt;b k&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;k&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" style="height: 15.0pt;"&gt;b k&lt;/TD&gt;
&lt;TD&gt;b&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" style="height: 15.0pt;"&gt;b y m&lt;/TD&gt;
&lt;TD&gt;b&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;y&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" style="height: 15.0pt;"&gt;b y m&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;m&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" style="height: 15.0pt;"&gt;c z&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;z&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" style="height: 15.0pt;"&gt;c x k&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;k&lt;/TD&gt;
&lt;TD&gt;x&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" style="height: 15.0pt;"&gt;a b&lt;/TD&gt;
&lt;TD&gt;a&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" style="height: 15.0pt;"&gt;a b&lt;/TD&gt;
&lt;TD&gt;b&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" style="height: 15.0pt;"&gt;a b&lt;/TD&gt;
&lt;TD&gt;a b&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;</description>
      <pubDate>Fri, 11 Oct 2019 04:12:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-columns-with-values-based-on-occurence-of-words/m-p/595623#M171392</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2019-10-11T04:12:06Z</dc:date>
    </item>
    <item>
      <title>Re: How to create columns with values based on occurence of words (from predefined lists) in a strin</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-columns-with-values-based-on-occurence-of-words/m-p/595681#M171418</link>
      <description>&lt;P&gt;This could give you a start .&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data strings ;                                                                                                                          
  input string $6. ;                                                                                                                    
  cards ;                                                                                                                               
a x                                                                                                                                     
aa l z                                                                                                                                  
b k                                                                                                                                     
b y m                                                                                                                                   
c z                                                                                                                                     
c x k                                                                                                                                   
a b                                                                                                                                     
;                                                                                                                                       
run ;                                                                                                                                   
                                                                                                                                        
data variables ;                                                                                                                        
  input (var1-var3) (&amp;amp; :$3.) ;                                                                                                          
  cards ;                                                                                                                               
a    k  x                                                                                                                               
b    l  y                                                                                                                               
a b  m  z                                                                                                                               
.    n  .                                                                                                                               
;                                                                                                                                       
run ;  
proc sql;
create table want as
select 	a.*,var1,var2,var3
 from strings as a left join variables(keep=var1) as b 
  on findw(a.string,strip(b.var1),' ')
left join variables(keep=var2) as c 
  on findw(a.string,strip(c.var2),' ')
left join variables(keep=var3) as d 
  on findw(a.string,strip(d.var3),' ')
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 11 Oct 2019 11:40:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-columns-with-values-based-on-occurence-of-words/m-p/595681#M171418</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2019-10-11T11:40:57Z</dc:date>
    </item>
    <item>
      <title>Re: How to create columns with values based on occurence of words (from predefined lists) in a strin</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-columns-with-values-based-on-occurence-of-words/m-p/595703#M171426</link>
      <description>&lt;P&gt;Hi ballardw,&lt;/P&gt;&lt;P&gt;My working data can contain many variable values that include other variable values as substrings.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here's a more complex example:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="image.png" style="width: 267px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/33067i4F4EF1A4D9ED490D/image-size/large?v=v2&amp;amp;px=999" role="button" title="image.png" alt="image.png" /&gt;&lt;/span&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="image.png" style="width: 361px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/33068i71A763F4192D902D/image-size/large?v=v2&amp;amp;px=999" role="button" title="image.png" alt="image.png" /&gt;&lt;/span&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="image.png" style="width: 563px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/33069i10FE95327A5C6A4E/image-size/large?v=v2&amp;amp;px=999" role="button" title="image.png" alt="image.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You made me realize more things that are necessary here, so all in all I would like:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;to output the longest match in case of multiple occurences of values from list in a string (e.g. "green beans Audi" outputs vegetables="green beans", not just "beans")&lt;/LI&gt;&lt;LI&gt;to make the scan case-insensitive (for string "bmw x5" the output is cars="BMW X5" in the same case as was in the variables table)&lt;/LI&gt;&lt;LI&gt;to make the code independent of the names of variables and their number - these can be changed and added, so it would be great to have a universal code that can deal with any names and any numebr of variables (such as cars, vegetables etc.)&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;,&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/21262"&gt;@hashman&lt;/a&gt;&amp;nbsp;how would you go about this?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In case you want to give it a try, here's the code for data:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data strings ;                                                                                                            
  input string $30.;                                                                                                      
  cards ;                                                                                                                 
Ford carrot
Honda beans
south america black beans
green beans Audi
x5 chili pepper lettuce
garlic north America
central europe bmw
lettuce onion X5
America pepper
X5 BMW Europe
Central East Europe
bmw X5 america
;                                                                                                                         
run ;  

data variables;
   infile datalines delimiter=',' dsd; 
   length cars $6 vegetables $12 regions $14;
   input cars $ vegetables $ regions $;
   datalines;                      
Audi,beans,America
BMW,black beans,Central Europe
BMW X5,carrot,East Europe
X5,chili pepper,Europe
Honda,green beans,North America
,lettuce,South America
,onion,
,pepper,
;&lt;BR /&gt;run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 11 Oct 2019 12:34:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-columns-with-values-based-on-occurence-of-words/m-p/595703#M171426</guid>
      <dc:creator>LuciaCekanakova</dc:creator>
      <dc:date>2019-10-11T12:34:16Z</dc:date>
    </item>
    <item>
      <title>Re: How to create columns with values based on occurence of words (from predefined lists) in a strin</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-columns-with-values-based-on-occurence-of-words/m-p/595708#M171431</link>
      <description>&lt;P&gt;I still don't get what you want to do with the multi-word strings in the VARIABLES table.&lt;/P&gt;
&lt;P&gt;Do you only want to match 'BMW X5' to STRINGS that contain 'BMW X5' in that order and not to strings that have 'X5 BMW' or 'BMW model X5'?&lt;/P&gt;
&lt;P&gt;Do you want to match also to strings that contain only 'BMW' or only 'X5'?&lt;/P&gt;</description>
      <pubDate>Fri, 11 Oct 2019 12:44:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-columns-with-values-based-on-occurence-of-words/m-p/595708#M171431</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-10-11T12:44:16Z</dc:date>
    </item>
    <item>
      <title>Re: How to create columns with values based on occurence of words (from predefined lists) in a strin</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-columns-with-values-based-on-occurence-of-words/m-p/595718#M171437</link>
      <description>&lt;P&gt;Do you only want to match 'BMW X5' to STRINGS that contain 'BMW X5' in that order and not to strings that have 'X5 BMW' or 'BMW model X5'? YES, exactly.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Do you want to match also to strings that contain only 'BMW' or only 'X5'? These are matched to values 'BMW' and 'X5' that are also in the variable list, but not because they are part of 'BMW X5' that is in this list. You basically always scan the string for the exact value in the variable list. And if the value list contains values 'BMW', 'X5' and 'BMW X5', a string like the one in observation #12 would match all of these, but I'd like the output to be only the longest exact match - which is 'BMW X5' in this case. In obs #10 it matches 'X5' and 'BMW', so 'BMW' goes into output.&lt;/P&gt;</description>
      <pubDate>Fri, 11 Oct 2019 13:10:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-columns-with-values-based-on-occurence-of-words/m-p/595718#M171437</guid>
      <dc:creator>LuciaCekanakova</dc:creator>
      <dc:date>2019-10-11T13:10:26Z</dc:date>
    </item>
    <item>
      <title>Re: How to create columns with values based on occurence of words (from predefined lists) in a strin</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-columns-with-values-based-on-occurence-of-words/m-p/595720#M171438</link>
      <description>&lt;P&gt;In that case use the FINDW() function.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql ;
  create table want as
    select string
         , case when (findw(string,cars,' ','sit')) then cars else ' ' end as cars
         , case when (findw(string,vegetables,' ','sit')) then vegetables else ' ' end as vegetables
         , case when (findw(string,regions,' ','sit')) then regions else ' ' end as regions
    from strings,variables
    having not missing(cats(calculated cars,calculated vegetables,calculated regions))
    order by 1,2,3,4
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 11 Oct 2019 13:12:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-columns-with-values-based-on-occurence-of-words/m-p/595720#M171438</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-10-11T13:12:02Z</dc:date>
    </item>
    <item>
      <title>Re: How to create columns with values based on occurence of words (from predefined lists) in a strin</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-columns-with-values-based-on-occurence-of-words/m-p/595722#M171440</link>
      <description>&lt;P&gt;This program finds all the matches and creates an observation for each match. How do I make it to select only the longest match for each variable?&lt;/P&gt;</description>
      <pubDate>Fri, 11 Oct 2019 13:17:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-columns-with-values-based-on-occurence-of-words/m-p/595722#M171440</guid>
      <dc:creator>LuciaCekanakova</dc:creator>
      <dc:date>2019-10-11T13:17:28Z</dc:date>
    </item>
    <item>
      <title>Re: How to create columns with values based on occurence of words (from predefined lists) in a strin</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-columns-with-values-based-on-occurence-of-words/m-p/595727#M171446</link>
      <description>&lt;P&gt;Probably easier to do each "variable" separately then.&amp;nbsp; I added row numbers to your sample dataset to make it easier to see what is happening. And to give a tiebreaker when there are multiple&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data strings ;
  input string $30.;
  snum+1;
cards;
Ford carrot
Honda beans
south america black beans
green beans Audi
x5 chili pepper lettuce
garlic north America
central europe bmw
lettuce onion X5
America pepper
X5 BMW Europe
Central East Europe
bmw X5 america
;

data variables;
   infile cards delimiter=',' dsd;
   length cars $6 vegetables $12 regions $14;
   input cars vegetables regions;
   vnum+1;
cards;
Audi,beans,America
BMW,black beans,Central Europe
BMW X5,carrot,East Europe
X5,chili pepper,Europe
Honda,green beans,North America
,lettuce,South America
,onion,
,pepper,
;

proc sql ;
  create table vegetables as
    select snum,string
         , lengthn(vegetables) as length
         , vnum,vegetables
    from strings left join variables
      on findw(string,vegetables,' ','sit')
    order by snum,string,length,snum
  ;
quit;

data vegetables;
  set vegetables;
  by snum;
  if last.snum;
run;

proc print data=vegetables width=min;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;Obs    snum    string                       length    vnum    vegetables

  1      1     Ford carrot                     6        3     carrot
  2      2     Honda beans                     5        1     beans
  3      3     south america black beans      11        2     black beans
  4      4     green beans Audi               11        5     green beans
  5      5     x5 chili pepper lettuce        12        4     chili pepper
  6      6     garlic north America            0        .
  7      7     central europe bmw              0        .
  8      8     lettuce onion X5                7        6     lettuce
  9      9     America pepper                  6        8     pepper
 10     10     X5 BMW Europe                   0        .
 11     11     Central East Europe             0        .
 12     12     bmw X5 america                  0        .
&lt;/PRE&gt;</description>
      <pubDate>Fri, 11 Oct 2019 13:46:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-columns-with-values-based-on-occurence-of-words/m-p/595727#M171446</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-10-11T13:46:11Z</dc:date>
    </item>
    <item>
      <title>Re: How to create columns with values based on occurence of words (from predefined lists) in a strin</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-columns-with-values-based-on-occurence-of-words/m-p/595736#M171453</link>
      <description>&lt;P&gt;For some reason my output from your code looks different:&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="image.png" style="width: 529px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/33072i334DEB64542466FD/image-size/large?v=v2&amp;amp;px=999" role="button" title="image.png" alt="image.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any idea why? There are no errors or warnings in the log.&lt;/P&gt;</description>
      <pubDate>Fri, 11 Oct 2019 13:53:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-columns-with-values-based-on-occurence-of-words/m-p/595736#M171453</guid>
      <dc:creator>LuciaCekanakova</dc:creator>
      <dc:date>2019-10-11T13:53:52Z</dc:date>
    </item>
    <item>
      <title>Re: How to create columns with values based on occurence of words (from predefined lists) in a strin</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-columns-with-values-based-on-occurence-of-words/m-p/595738#M171454</link>
      <description>Copy the code again as I did change the ordering and the IF statement to make sure it is taking the longest match.&lt;BR /&gt;</description>
      <pubDate>Fri, 11 Oct 2019 14:08:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-columns-with-values-based-on-occurence-of-words/m-p/595738#M171454</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-10-11T14:08:32Z</dc:date>
    </item>
    <item>
      <title>Re: How to create columns with values based on occurence of words (from predefined lists) in a strin</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-columns-with-values-based-on-occurence-of-words/m-p/595861#M171492</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/190130"&gt;@LuciaCekanakova&lt;/a&gt;:&lt;/P&gt;
&lt;P&gt;Well, in this case my program in its core remains the same, except:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;In addition to STRING, each variable from VARIABLES has to be scanned&lt;/LI&gt;
&lt;LI&gt;A provisions must be made for storing the (sub) strings from VARIABLES in their original (prop)case for further retrieval&lt;/LI&gt;
&lt;LI&gt;Logic for selecting the longest matching substring has to be added&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;The rest is just diligent, even if somewhat tedious, coding:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data strings ;                                                                                                                          
  input string $30.;                                                                                                                    
  cards ;                                                                                                                               
Ford carrot                                                                                                                             
Honda beans                                                                                                                             
south america black beans                                                                                                               
green beans Audi                                                                                                                        
x5 chili pepper lettuce                                                                                                                 
garlic north America                                                                                                                    
central europe bmw                                                                                                                      
lettuce onion X5                                                                                                                        
America pepper                                                                                                                          
X5 BMW Europe                                                                                                                           
Central East Europe                                                                                                                     
bmw X5 america                                                                                                                          
;                                                                                                                                       
run ;                                                                                                                                   
                                                                                                                                        
data variables;                                                                                                                         
   infile datalines delimiter=',' dsd;                                                                                                  
   length cars $6 vegetables $12 regions $14;                                                                                           
   input cars $ vegetables $ regions $;                                                                                                 
   datalines;                                                                                                                           
Audi,beans,America                                                                                                                      
BMW,black beans,Central Europe                                                                                                          
BMW X5,carrot,East Europe                                                                                                               
X5,chili pepper,Europe                                                                                                                  
Honda,green beans,North America                                                                                                         
,lettuce,South America                                                                                                                  
,onion,                                                                                                                                 
,pepper,                                                                                                                                
;                                                                                                                                       
run;                                                                                                                                    
                                                                                                                                        
proc sql noprint ;                                                                                                                      
  select max (length) into :maxvlen from dictionary.columns                                                                             
  where libname="WORK" and memname="VARIABLES"                                                                                          
  ;                                                                                                                                     
quit ;                                                                                                                                  
                                                                                                                                        
data want (drop = _:) ;                                                                                                                 
  set strings ;                                                                                                                         
  if _n_ = 1 then do ;                                                                                                                  
    length _s _z $ &amp;amp;maxvlen ;                                                                                                           
    dcl hash h () ;                                                                                                                     
    h.definekey ("_s") ;                                                                                                                
    h.definedata ("_z", "_i_") ;                                                                                                        
    h.definedone () ;                                                                                                                   
    do until (z) ;                                                                                                                      
      set variables end = z ;                                                                                                           
      array v cars vegetables regions ;                                                                                                 
      do over v ;                                                                                                                       
        if cmiss (v) then continue ;                                                                                                    
        do _i = 1 to countw (v) ;                                                                                                       
          _z = scan (v, _i) ;                                                                                                           
          h.ref (key: upcase (_z), data: _z, data: _i_) ;                                                                               
          do _j = _i + 1 to countw (v) ;                                                                                                
            _z = catx (" ", _z, scan (v, _j)) ;                                                                                         
            h.ref (key: upcase (_z), data: _z, data: _i_) ;                                                                             
          end ;                                                                                                                         
        end ;                                                                                                                           
      end ;                                                                                                                             
    end ;                                                                                                                               
  end ;                                                                                                                                 
  call missing (of v[*]) ;                                                                                                              
  do _i = 1 to countw (string) ;                                                                                                        
    _s = scan (string, _i) ;                                                                                                            
    if h.find (key: upcase (_s)) = 0 then if length (_z) &amp;gt; length (v) then v = _z ;                                                     
    do _j = _i + 1 to countw (string) ;                                                                                                 
      _s = catx (" ", _s, scan (string, _j)) ;                                                                                          
      if h.find (key: upcase (_s)) = 0 then if length (_z) &amp;gt; length (v) then v = _z ;                                                   
    end ;                                                                                                                               
  end ;                                                                                                                                 
run ;                                     
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 11 Oct 2019 19:56:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-columns-with-values-based-on-occurence-of-words/m-p/595861#M171492</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-10-11T19:56:33Z</dc:date>
    </item>
    <item>
      <title>Re: How to create columns with values based on occurence of words (from predefined lists) in a strin</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-columns-with-values-based-on-occurence-of-words/m-p/596250#M171647</link>
      <description>&lt;P&gt;Thank you! This is exactly what I need &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;&lt;P&gt;The only thing I changed is that I created a macro variable with the list of variable names and then used it in array "v".&lt;/P&gt;</description>
      <pubDate>Mon, 14 Oct 2019 12:54:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-columns-with-values-based-on-occurence-of-words/m-p/596250#M171647</guid>
      <dc:creator>LuciaCekanakova</dc:creator>
      <dc:date>2019-10-14T12:54:28Z</dc:date>
    </item>
    <item>
      <title>Re: How to create columns with values based on occurence of words (from predefined lists) in a strin</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-columns-with-values-based-on-occurence-of-words/m-p/596320#M171681</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/190130"&gt;@LuciaCekanakova&lt;/a&gt;:&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&amp;gt;I created a macro variable with the list of variable names and then used it in array "v"&amp;lt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Given that you'd been striving to make the program as little hard coded as possible, this totally makes sense.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 14 Oct 2019 16:23:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-columns-with-values-based-on-occurence-of-words/m-p/596320#M171681</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-10-14T16:23:45Z</dc:date>
    </item>
    <item>
      <title>Re: How to create columns with values based on occurence of words (from predefined lists) in a strin</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-columns-with-values-based-on-occurence-of-words/m-p/599344#M172999</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/21262"&gt;@hashman&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hi, I discovered 2 more things that aren't working as I expected:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;When the VARIABLES table contains mulitple-word values, but the partial values are not in the value list (e.g. "South America" is there, but "South" by itself is not) they are also searched for in the STRINGS and if they are found, they are output in the new column (see the column regions in obs #2 and #8). I'd like to stop this from happening adn only search for the whole values if they have multiple-words.&lt;/LI&gt;&lt;LI&gt;In VARIABLES table it happens that the same value is in multiple columns (e.g. beans, carrot... - I added a new column with the general type of vegetable). When this occurs, the found value is output only in one variable column it was found in, it doesn't repeat in others (see columns vegetable and vegetablegeneral in obs #1,2,8,9). I'd like for these values to repeat in all the output column were they should be base don the original VARIABLES columns.&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;My output:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="image.png" style="width: 571px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/33400i35C99BE897081B58/image-size/large?v=v2&amp;amp;px=999" role="button" title="image.png" alt="image.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Desired output:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="image.png" style="width: 569px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/33399i8C312B4E7CE75E85/image-size/large?v=v2&amp;amp;px=999" role="button" title="image.png" alt="image.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please, do you have any tip on how to modify the code to achieve this?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The code so far with new data:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data strings ;                                                                                                                          
  input string $30.;                                                                                                                    
  cards ;                                                                                                                               
Ford carrot                                                                                                                             
Honda beans South Africa                                                                                                                             
south america black beans                                                                                                               
green beans Audi                                                                                                                        
x5 chili pepper                                                                                                   
garlic north America                                                                                                                    
central europe bmw                                                                                                                      
lettuce north onion X5                                                                                                                        
America pepper                                                                                                                     
X5 BMW Europe                                                                                                                           
Central East Europe                                                                                                                     
bmw X5 america                                                                                                                          
;                                                                                                                                       
run ;                                                                                                                                   
                                                                                                                                        
data variables;                                                                                                                         
   infile datalines delimiter=',' dsd;                                                                                                  
   length cars $6 vegetablegeneral $7 vegetable $12 regions $14;                                                                                           
   input cars $ vegetablegeneral $ vegetable $ regions $;                                                                                                 
   datalines;                                                                                                                           
Audi,beans,beans,America                                                                                                                      
BMW,carrot,black beans,Central Europe                                                                                                          
BMW X5,lettuce,carrot,East Europe                                                                                                               
X5,onion,chili pepper,Europe                                                                                                                  
Honda,pepper,green beans,North America                                                                                                         
,,lettuce,South America                                                                                                                  
,,onion,                                                                                                                                 
,,pepper,                                                                                                                                
;                                                                                                                                       
run;                                                                                                                                    
                                                                                                                                        
proc sql noprint ;                                                                                                                      
  select max (length) into :maxvlen from dictionary.columns                                                                             
  where libname="WORK" and memname="VARIABLES"                                                                                          
  ;                                                                                                                                     
quit ;      

proc sql noprint;                                                                                                                      
  select name into :varlist separated by ' ' from dictionary.columns
  where libname="WORK" and memname="VARIABLES";
quit;   
                                                                                                                                        
data want (drop = _:) ;                                                                                                                 
  set strings ;                                                                                                                         
  if _n_ = 1 then do ;                                                                                                                  
    length _s _z $ &amp;amp;maxvlen ;                                                                                                           
    dcl hash h () ;                                                                                                                     
    h.definekey ("_s") ;                                                                                                                
    h.definedata ("_z", "_i_") ;                                                                                                        
    h.definedone () ;                                                                                                                   
    do until (z) ;                                                                                                                      
      set variables end = z ;                                                                                                           
      array v &amp;amp;varlist ;                                                                                                 
      do over v ;                                                                                                                       
        if cmiss (v) then continue ;                                                                                                    
        do _i = 1 to countw (v) ;                                                                                                       
          _z = scan (v, _i) ;                                                                                                           
          h.ref (key: upcase (_z), data: _z, data: _i_) ;                                                                               
          do _j = _i + 1 to countw (v) ;                                                                                                
            _z = catx (" ", _z, scan (v, _j)) ;                                                                                         
            h.ref (key: upcase (_z), data: _z, data: _i_) ;                                                                             
          end ;                                                                                                                         
        end ;                                                                                                                           
      end ;                                                                                                                             
    end ;                                                                                                                               
  end ;                                                                                                                                 
  call missing (of v[*]) ;                                                                                                              
  do _i = 1 to countw (string) ;                                                                                                        
    _s = scan (string, _i) ;                                                                                                            
    if h.find (key: upcase (_s)) = 0 then if length (_z) &amp;gt; length (v) then v = _z ;                                                     
    do _j = _i + 1 to countw (string) ;                                                                                                 
      _s = catx (" ", _s, scan (string, _j)) ;                                                                                          
      if h.find (key: upcase (_s)) = 0 then if length (_z) &amp;gt; length (v) then v = _z ;                                                   
    end ;                                                                                                                               
  end ;                                                                                                                                 
run ;    &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 25 Oct 2019 14:26:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-columns-with-values-based-on-occurence-of-words/m-p/599344#M172999</guid>
      <dc:creator>LuciaCekanakova</dc:creator>
      <dc:date>2019-10-25T14:26:37Z</dc:date>
    </item>
    <item>
      <title>Re: How to create columns with values based on occurence of words (from predefined lists) in a strin</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-columns-with-values-based-on-occurence-of-words/m-p/599559#M173082</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/190130"&gt;@LuciaCekanakova&lt;/a&gt;: Will take a look when I get a piece of free quality time.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 26 Oct 2019 19:00:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-columns-with-values-based-on-occurence-of-words/m-p/599559#M173082</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-10-26T19:00:49Z</dc:date>
    </item>
    <item>
      <title>Re: How to create columns with values based on occurence of words (from predefined lists) in a strin</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-columns-with-values-based-on-occurence-of-words/m-p/607236#M176458</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/21262"&gt;@hashman&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;&lt;P&gt;any luck with the free time/the code yet? &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 26 Nov 2019 09:08:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-columns-with-values-based-on-occurence-of-words/m-p/607236#M176458</guid>
      <dc:creator>LuciaCekanakova</dc:creator>
      <dc:date>2019-11-26T09:08:30Z</dc:date>
    </item>
    <item>
      <title>Re: How to create columns with values based on occurence of words (from predefined lists) in a strin</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-columns-with-values-based-on-occurence-of-words/m-p/608532#M177114</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/190130"&gt;@LuciaCekanakova&lt;/a&gt;: Sorry, totally swamped now.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 01 Dec 2019 03:56:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-columns-with-values-based-on-occurence-of-words/m-p/608532#M177114</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-12-01T03:56:47Z</dc:date>
    </item>
  </channel>
</rss>

