<?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: Using array to recode multiple variables into one variable in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Using-array-to-recode-multiple-variables-into-one-variable/m-p/744066#M29291</link>
    <description>I'm really new to SAS so I'm not exactly sure what a mapping table is? I will need to scale this up because I have more categories and will be creating more variables using the code based on this post.</description>
    <pubDate>Wed, 26 May 2021 23:20:04 GMT</pubDate>
    <dc:creator>monsterpie</dc:creator>
    <dc:date>2021-05-26T23:20:04Z</dc:date>
    <item>
      <title>Using array to recode multiple variables into one variable</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Using-array-to-recode-multiple-variables-into-one-variable/m-p/744054#M29288</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have 5 variables (bev_1-bev_5) that are open text responses. I am trying to create a new variable for each bev_1-bev_5 which classifies the responses in each column as either 1 or 2. The first new variable that I want to create, called product_type&amp;nbsp; should have 2 levels: if "Coca-Cola" or "Pepsi" are found in bev_1-bev_5 then the the variable &lt;EM&gt;product&lt;/EM&gt; should =1. If "Hamburger" is found in any observations of bev_1-bev_5 then product should =2.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;An example of my dataset is below. Variables product_1-product_5 are the new variables I am trying to create. Product_1 classifies the responses under bev_1, product_2 classifies responses of bev_2, etc.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&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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;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is an example of the progress I've made so far with my code. This code runs but ouputs only one variable and identifys which column "Coke" or "Pepsi" is located.&amp;nbsp; I'm also not sure if an array is the most appropriate function to use in this case. Any help is appreciated!&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
array x{*} bev_1-bev_5;
product_type=whichc("Coke","Pepsi",of x{*});
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 26 May 2021 21:56:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Using-array-to-recode-multiple-variables-into-one-variable/m-p/744054#M29288</guid>
      <dc:creator>monsterpie</dc:creator>
      <dc:date>2021-05-26T21:56:34Z</dc:date>
    </item>
    <item>
      <title>Re: Using array to recode multiple variables into one variable</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Using-array-to-recode-multiple-variables-into-one-variable/m-p/744056#M29289</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  array x{*} bev_1-bev_5;
  array product_ {5};
  do i=1 to 5;
    if x{i} in ('Coke','Pepsi') then product_{i}=1;
    else if x{i}='Hamburger' then product_{i}=2;
  end;
  drop i;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;ARRAY is the right tool to use, but you should also implement a do loop over the array elements:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 27 May 2021 12:10:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Using-array-to-recode-multiple-variables-into-one-variable/m-p/744056#M29289</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2021-05-27T12:10:25Z</dc:date>
    </item>
    <item>
      <title>Re: Using array to recode multiple variables into one variable</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Using-array-to-recode-multiple-variables-into-one-variable/m-p/744061#M29290</link>
      <description>Do you have a mapping table for these values? If it's just the two sets of conditions it's not hard but this won't scale well if you have more data and categories. If you had a table that maps the value, Coke = 1, Pepsi=1, Hamburger=2 in a data set then this could be done nicely with a format and array. &lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Wed, 26 May 2021 22:30:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Using-array-to-recode-multiple-variables-into-one-variable/m-p/744061#M29290</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-05-26T22:30:08Z</dc:date>
    </item>
    <item>
      <title>Re: Using array to recode multiple variables into one variable</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Using-array-to-recode-multiple-variables-into-one-variable/m-p/744066#M29291</link>
      <description>I'm really new to SAS so I'm not exactly sure what a mapping table is? I will need to scale this up because I have more categories and will be creating more variables using the code based on this post.</description>
      <pubDate>Wed, 26 May 2021 23:20:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Using-array-to-recode-multiple-variables-into-one-variable/m-p/744066#M29291</guid>
      <dc:creator>monsterpie</dc:creator>
      <dc:date>2021-05-26T23:20:04Z</dc:date>
    </item>
    <item>
      <title>Re: Using array to recode multiple variables into one variable</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Using-array-to-recode-multiple-variables-into-one-variable/m-p/744171#M29294</link>
      <description>A look up table, that has the original values and the values you want them to be. Basically do you have a table that looks like:&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Code Product&lt;BR /&gt;1 Coke&lt;BR /&gt;1 Pepsi&lt;BR /&gt;2 Hamburger&lt;BR /&gt;....&lt;BR /&gt;&lt;BR /&gt;Use a format, see this document on how Formats work:&lt;BR /&gt;&lt;A href="https://support.sas.com/resources/papers/proceedings/proceedings/sugi30/001-30.pdf" target="_blank"&gt;https://support.sas.com/resources/papers/proceedings/proceedings/sugi30/001-30.pdf&lt;/A&gt;&lt;BR /&gt;&lt;A href="https://support.sas.com/resources/papers/proceedings/proceedings/sugi30/001-30.pdf" target="_blank"&gt;https://support.sas.com/resources/papers/proceedings/proceedings/sugi30/001-30.pdf&lt;/A&gt;</description>
      <pubDate>Thu, 27 May 2021 14:51:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Using-array-to-recode-multiple-variables-into-one-variable/m-p/744171#M29294</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-05-27T14:51:29Z</dc:date>
    </item>
  </channel>
</rss>

