<?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: Use SAS to create a lookup table with key values in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Use-SAS-to-create-a-lookup-table-with-key-values/m-p/744298#M29298</link>
    <description>&lt;P&gt;As a pair for FORMATS (or INFORMATS) and not as a table at all.&lt;/P&gt;
&lt;P&gt;Formats convert values to text.&amp;nbsp; Informats convert text to values.&amp;nbsp; If you want new PRODUCT_TYPE and TYPE_OF_DRINK values to be numbers then define INFORMATs and use the INPUT() function to convert the strings in BEV_1 to BEV_5 into numbers.&amp;nbsp; &amp;nbsp;But if you are ok with the results being strings you can just apply the format to BEV_1 to BEV_5 without having to modify the original table at all.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format ;
value $product 
  'Coke', 'Coca-cola', 'Cocacola'='1'
  'burger'='2'
  'redbull', 'Red Bull', 'RedBull'='1'
  other = 'Unknown'
;
value $drink 
  'Coke', 'Coca-cola', 'Cocacola'='1'
  'burger'='0'
  'redbull', 'Red Bull', 'RedBull'='3'
  other='Unknown'
;
run;

data have;
  input ID (bev_1-bev_5) (:$10.) ;
cards;
1 Coke Starbucks Pepsi CocaCola fries
2 Coca-Cola burger pepsi coke redbull
;

proc print data=have;
  format bev: $product.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results:&lt;/P&gt;
&lt;PRE&gt;Obs    ID    bev_1      bev_2       bev_3      bev_4     bev_5

 1      1    1          Unknown    Unknown    Unknown    Unknown
 2      2    Unknown    2          Unknown    Unknown    1
&lt;/PRE&gt;</description>
    <pubDate>Thu, 27 May 2021 20:39:38 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2021-05-27T20:39:38Z</dc:date>
    <item>
      <title>Use SAS to create a lookup table with key values</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Use-SAS-to-create-a-lookup-table-with-key-values/m-p/744296#M29297</link>
      <description>&lt;P&gt;Hello all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a dataset with 1000 observations and 6 variables (below is an example). The values of the variables bev_1-bev_5 are open ended text and contain different spelling variations. I'm creating additional variables which classify the responses in each bev_1-bev_5 column as 1=drink or 2=food and the type of drink. My goal is to set up a lookup table or essentially another dataset which includes all of the values of bev_1-bev_5 and the corresponding codes for my new variables. This lookup table will also have to include the spelling variations (e.g., Coke, Coca-cola, CocaCola, coke, etc. should all be coded as 1 for my product_Type variable, and should all be coded the same for any other variables I want to add).&lt;/P&gt;&lt;P&gt;Example dataset:&lt;/P&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;ID&lt;/TD&gt;&lt;TD&gt;bev_1&lt;/TD&gt;&lt;TD&gt;bev_2&lt;/TD&gt;&lt;TD&gt;bev_3&lt;/TD&gt;&lt;TD&gt;bev_4&lt;/TD&gt;&lt;TD&gt;bev_5&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;Coke&lt;/TD&gt;&lt;TD&gt;Starbucks&lt;/TD&gt;&lt;TD&gt;Pepsi&lt;/TD&gt;&lt;TD&gt;CocaCola&lt;/TD&gt;&lt;TD&gt;fries&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;Coca-Cola&lt;/TD&gt;&lt;TD&gt;burger&lt;/TD&gt;&lt;TD&gt;pepsi&lt;/TD&gt;&lt;TD&gt;coke&lt;/TD&gt;&lt;TD&gt;redbull&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Question: &lt;/STRONG&gt;I'm wondering what the best way would be to structure the lookup table so that for example, anytime SAS sees the word "Coke" or any of its variations (which I would include in my lookup table) in bev_1-bev_5, it will code it appropriately based on the new variables I'd like to add.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Lookup table that I want to link, essentially as a "dictionary" to my original dataset. Columns are the new variables I'll be adding:&lt;/P&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Response and variations&lt;/TD&gt;&lt;TD&gt;product_type&lt;/TD&gt;&lt;TD&gt;type of drink&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Coke, Coca-cola, Cocacola&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;burger&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;redbull, Red Bull, RedBull&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Thu, 27 May 2021 20:25:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Use-SAS-to-create-a-lookup-table-with-key-values/m-p/744296#M29297</guid>
      <dc:creator>monsterpie</dc:creator>
      <dc:date>2021-05-27T20:25:31Z</dc:date>
    </item>
    <item>
      <title>Re: Use SAS to create a lookup table with key values</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Use-SAS-to-create-a-lookup-table-with-key-values/m-p/744298#M29298</link>
      <description>&lt;P&gt;As a pair for FORMATS (or INFORMATS) and not as a table at all.&lt;/P&gt;
&lt;P&gt;Formats convert values to text.&amp;nbsp; Informats convert text to values.&amp;nbsp; If you want new PRODUCT_TYPE and TYPE_OF_DRINK values to be numbers then define INFORMATs and use the INPUT() function to convert the strings in BEV_1 to BEV_5 into numbers.&amp;nbsp; &amp;nbsp;But if you are ok with the results being strings you can just apply the format to BEV_1 to BEV_5 without having to modify the original table at all.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format ;
value $product 
  'Coke', 'Coca-cola', 'Cocacola'='1'
  'burger'='2'
  'redbull', 'Red Bull', 'RedBull'='1'
  other = 'Unknown'
;
value $drink 
  'Coke', 'Coca-cola', 'Cocacola'='1'
  'burger'='0'
  'redbull', 'Red Bull', 'RedBull'='3'
  other='Unknown'
;
run;

data have;
  input ID (bev_1-bev_5) (:$10.) ;
cards;
1 Coke Starbucks Pepsi CocaCola fries
2 Coca-Cola burger pepsi coke redbull
;

proc print data=have;
  format bev: $product.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results:&lt;/P&gt;
&lt;PRE&gt;Obs    ID    bev_1      bev_2       bev_3      bev_4     bev_5

 1      1    1          Unknown    Unknown    Unknown    Unknown
 2      2    Unknown    2          Unknown    Unknown    1
&lt;/PRE&gt;</description>
      <pubDate>Thu, 27 May 2021 20:39:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Use-SAS-to-create-a-lookup-table-with-key-values/m-p/744298#M29298</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-05-27T20:39:38Z</dc:date>
    </item>
    <item>
      <title>Re: Use SAS to create a lookup table with key values</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Use-SAS-to-create-a-lookup-table-with-key-values/m-p/744300#M29299</link>
      <description>&lt;P&gt;Thanks for your help Tom! As a follow up question, the reason why I was thinking of doing a table of some sort was because my dataset values are open text, with the different spelling variations I have thousands of unique responses. So i was trying to avoid having to manually type out each spelling variation as part of a SAS code but rather create a csv table which I could import into SAS and then link that csv to the following code that I was using to actually create the new variables I want.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
	array x{*} bev_1-bev_5;
	array product_type {5};
		if x{i} in ('Coke', 'Pepsi') then product_type{i}=1;
		else if x{i}='burger' then product_type{i}=2;
	end;
	drop i;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 27 May 2021 20:52:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Use-SAS-to-create-a-lookup-table-with-key-values/m-p/744300#M29299</guid>
      <dc:creator>monsterpie</dc:creator>
      <dc:date>2021-05-27T20:52:20Z</dc:date>
    </item>
    <item>
      <title>Re: Use SAS to create a lookup table with key values</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Use-SAS-to-create-a-lookup-table-with-key-values/m-p/744301#M29300</link>
      <description>You can create format/informat from data.  Look at the CNTLIN= option of PROC FORMAT.&lt;BR /&gt;</description>
      <pubDate>Thu, 27 May 2021 20:58:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Use-SAS-to-create-a-lookup-table-with-key-values/m-p/744301#M29300</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-05-27T20:58:56Z</dc:date>
    </item>
    <item>
      <title>Re: Use SAS to create a lookup table with key values</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Use-SAS-to-create-a-lookup-table-with-key-values/m-p/744305#M29301</link>
      <description>1. Transpose your data so that you have all values in one column (bev1-bev5 =&amp;gt; beverage via PROC TRANSPOSE)&lt;BR /&gt;2. Upcase/lower case all values (upper/lower), remove extra characters with COMPRESS() -&amp;gt; this way COKE=coke=COke and Coca-Cola = cocacola.  &lt;BR /&gt;3. Sort and remove duplicates (PROC SORT + NODUPKEY)&lt;BR /&gt;4. Export to Excel/CSV&lt;BR /&gt;5. Clean file or organize such that you have something as follows:&lt;BR /&gt;&lt;BR /&gt;Response ProductType DrinkType&lt;BR /&gt;coke  1 1&lt;BR /&gt;cocacola 1 1&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;6. Reimport the file&lt;BR /&gt;7. Create a data set using CNTLIN - think there was an example in the last paper I linked you too - Title: PROC FORMAT: Not Just Another Pretty Face.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Thu, 27 May 2021 21:12:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Use-SAS-to-create-a-lookup-table-with-key-values/m-p/744305#M29301</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-05-27T21:12:53Z</dc:date>
    </item>
    <item>
      <title>Re: Use SAS to create a lookup table with key values</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Use-SAS-to-create-a-lookup-table-with-key-values/m-p/744699#M29320</link>
      <description>&lt;P&gt;Thanks Reeza for your input. I've followed all of the steps as you described but I seem to still be stuck on linking the dataset I import from excel with my original dataset after using the cntlin function. This is the SAS code I've used:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc transpose data=test out=bev_sample;
by id;
var bev_1-bev_5;
run;

*change all values to uppercase;
data bev_sample;
set bev_sample;
beverage=upcase(col1);
run;

*removing extra characters in values of COL1;
data bev_control;
set bev_sample;
beverage=compress(beverage);
run;

proc sort data=bev_control nodupkey;
by id;
run;

*setting fmtname, start, and label variables from bev_format dataset;
data bev_frmt;
rename id=start;
set bev_new;
label=map_code;
fmtname='$beverages';
run;

proc format cntlin=bev_frmt;
run;

*perform look up by reading in original dataset using the format I've created;
data bev_final;
set bev_control (where=(put(ID,$beverages.)='COKE'));
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Also just to clarify, this is an example of how I organized the excel file I read in as the format dataset:&lt;/P&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;ID&lt;/TD&gt;&lt;TD&gt;_NAME_&lt;/TD&gt;&lt;TD&gt;COL1&lt;/TD&gt;&lt;TD&gt;beverage&lt;/TD&gt;&lt;TD&gt;map_code&lt;/TD&gt;&lt;TD&gt;Response&lt;/TD&gt;&lt;TD&gt;product_type&lt;/TD&gt;&lt;TD&gt;drink_type&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;03&lt;/TD&gt;&lt;TD&gt;bev_1&lt;/TD&gt;&lt;TD&gt;Coca cola&lt;/TD&gt;&lt;TD&gt;COCACOLA&lt;/TD&gt;&lt;TD&gt;COKE&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;04&lt;/TD&gt;&lt;TD&gt;bev_1&lt;/TD&gt;&lt;TD&gt;Coke&lt;/TD&gt;&lt;TD&gt;COKE&lt;/TD&gt;&lt;TD&gt;COKE&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;05&lt;/TD&gt;&lt;TD&gt;bev_2&lt;/TD&gt;&lt;TD&gt;Hamburger&lt;/TD&gt;&lt;TD&gt;HAMBURGER&lt;/TD&gt;&lt;TD&gt;HAMBURGER&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;77&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Sun, 30 May 2021 20:43:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Use-SAS-to-create-a-lookup-table-with-key-values/m-p/744699#M29320</guid>
      <dc:creator>monsterpie</dc:creator>
      <dc:date>2021-05-30T20:43:57Z</dc:date>
    </item>
    <item>
      <title>Re: Use SAS to create a lookup table with key values</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Use-SAS-to-create-a-lookup-table-with-key-values/m-p/744793#M29328</link>
      <description>You still need to use the array/loop from before but now instead of a thousand different IF/THEN statements your loop becomes a single one.&lt;BR /&gt;&lt;BR /&gt;do i=1 to dim(bev);&lt;BR /&gt;_new_value(i) = put(bev(i), $beverages.);&lt;BR /&gt;end;&lt;BR /&gt;&lt;BR /&gt;If you want to map multiple values for the same code, ie response, product_type, drink_type then a merge may be a better solution. What you would do in that case is take your transposed data set (bev_sample) and merge with the lookup table you created for the format. Then you can use PROC TRANSPOSE to reflip it back to a wider format, if desired. Hopefully you're realizing that data in a long format can be much easier to work with.&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Mon, 31 May 2021 15:56:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Use-SAS-to-create-a-lookup-table-with-key-values/m-p/744793#M29328</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-05-31T15:56:13Z</dc:date>
    </item>
  </channel>
</rss>

