<?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 recode a categorical variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-recode-a-categorical-variable/m-p/563434#M157960</link>
    <description>I used If/then. It is like that If/then statement totally does not work. Normally, if and then suppose to go blue if the statement works. But they remains faded and when I run it nothing happen. Do you have any idea why it is not working at all? I am using SAS university edition for the first time.</description>
    <pubDate>Tue, 04 Jun 2019 03:54:12 GMT</pubDate>
    <dc:creator>SSin</dc:creator>
    <dc:date>2019-06-04T03:54:12Z</dc:date>
    <item>
      <title>How to recode a categorical variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-recode-a-categorical-variable/m-p/563125#M157819</link>
      <description>&lt;P&gt;I have a data set which contains Character variable "Housing_type". The response to that variables are "3-room", "4-room", "5-room", "Condominium", "Landed property" and "Other".&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like to recode&amp;nbsp; 3-room, 4-room and 5-room&amp;nbsp; into "HDB" and Condominium, Landed Property and Other into "Non-HDB".&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How may I do so please?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 02 Jun 2019 08:46:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-recode-a-categorical-variable/m-p/563125#M157819</guid>
      <dc:creator>SSin</dc:creator>
      <dc:date>2019-06-02T08:46:23Z</dc:date>
    </item>
    <item>
      <title>Re: How to recode a categorical variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-recode-a-categorical-variable/m-p/563133#M157821</link>
      <description>Have you tried using IF THEN statements in a DATA step?  You will be using them virtually every day, so it's a good time to learn them.  Give it a shot and post the results if you have any difficulty.</description>
      <pubDate>Sun, 02 Jun 2019 13:11:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-recode-a-categorical-variable/m-p/563133#M157821</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2019-06-02T13:11:11Z</dc:date>
    </item>
    <item>
      <title>Re: How to recode a categorical variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-recode-a-categorical-variable/m-p/563159#M157830</link>
      <description>&lt;P&gt;You can use IF/THEN to recode variables. The general structure is as follows:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;length new_housing_type $12.;

if housing_type  in ('3-room'  '4-room' '5-room') then new_housing_type = 'HBD';
else if housing_type in ("Condo") then new_housing_type = 'Condo';
else new_housing_type = 'Other';&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You'll need to modify it to match your exact cases. Note these are case sensitive comparisons.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/245291"&gt;@SSin&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I have a data set which contains Character variable "Housing_type". The response to that variables are "3-room", "4-room", "5-room", "Condominium", "Landed property" and "Other".&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would like to recode&amp;nbsp; 3-room, 4-room and 5-room&amp;nbsp; into "HDB" and Condominium, Landed Property and Other into "Non-HDB".&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How may I do so please?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 02 Jun 2019 23:08:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-recode-a-categorical-variable/m-p/563159#M157830</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-06-02T23:08:05Z</dc:date>
    </item>
    <item>
      <title>Re: How to recode a categorical variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-recode-a-categorical-variable/m-p/563282#M157889</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/245291"&gt;@SSin&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I have a data set which contains Character variable "Housing_type". The response to that variables are "3-room", "4-room", "5-room", "Condominium", "Landed property" and "Other".&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would like to recode&amp;nbsp; 3-room, 4-room and 5-room&amp;nbsp; into "HDB" and Condominium, Landed Property and Other into "Non-HDB".&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How may I do so please?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Another approach is to use a custom format, which if you do this frequently may be a better solution. Example:&lt;/P&gt;
&lt;PRE&gt;Proc format library=work;
value $ht
"3-room", "4-room", "5-room" = 'HDB'
"Condominium", "Landed property" , "Other" = 'Non-HDB'
;

data want;
  set have;
  ht = put(housing_type,$ht.);
run;&lt;/PRE&gt;
&lt;P&gt;Or try&lt;/P&gt;
&lt;PRE&gt;Proc Print data=have;
   format housing_type $ht.;
run;&lt;/PRE&gt;
&lt;P&gt;An advantage of a custom format is that if you find that you need to add an item, such as possibly "6-room" then you only need to add it to the format definition and rerun the proc format code. No need to add extra if/then/else.&lt;/P&gt;
&lt;P&gt;If using the format directly with the variable instead of creating an additional variable then you have the option to use the groups dynamically in most analysis, reporting or graphing procedures.&lt;/P&gt;
&lt;P&gt;Such as:&lt;/P&gt;
&lt;PRE&gt;Proc format library=work;
value $ht
"3-room", "4-room", "5-room" = 'HDB'
"Condominium", "Landed property" , "Other" = 'Non-HDB'
;
value $ht_3lvl
"2-room","3-room"='Small HDB' 
"4-room", "5-room" = 'Large HDB'
"Condominium", "Landed property" , "Other" = 'Non-HDB'
;
run;
Proc freq data=have;
   tables housing_type;
   format housing_type $ht_3lvl.;
run;
&lt;/PRE&gt;
&lt;P&gt;I of course have no idea if "Small" or "Large" actually apply to your need, that is just an example.&lt;/P&gt;
&lt;P&gt;Since your data likely does not have any 2-room from your description the "Small" would just be 3-room.&lt;/P&gt;</description>
      <pubDate>Mon, 03 Jun 2019 15:04:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-recode-a-categorical-variable/m-p/563282#M157889</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-06-03T15:04:36Z</dc:date>
    </item>
    <item>
      <title>Re: How to recode a categorical variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-recode-a-categorical-variable/m-p/563431#M157957</link>
      <description>Many thanks!!&lt;BR /&gt;</description>
      <pubDate>Tue, 04 Jun 2019 03:47:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-recode-a-categorical-variable/m-p/563431#M157957</guid>
      <dc:creator>SSin</dc:creator>
      <dc:date>2019-06-04T03:47:59Z</dc:date>
    </item>
    <item>
      <title>Re: How to recode a categorical variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-recode-a-categorical-variable/m-p/563432#M157958</link>
      <description>Many thanks!!</description>
      <pubDate>Tue, 04 Jun 2019 03:48:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-recode-a-categorical-variable/m-p/563432#M157958</guid>
      <dc:creator>SSin</dc:creator>
      <dc:date>2019-06-04T03:48:29Z</dc:date>
    </item>
    <item>
      <title>Re: How to recode a categorical variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-recode-a-categorical-variable/m-p/563433#M157959</link>
      <description>I used If/then. It is like that If/then statement totally does not work. Normally, if and then suppose to go blue if the statement works. But they remains faded and when I run it nothing happen. That is why. I am using SAS university edition for the first time.&lt;BR /&gt;</description>
      <pubDate>Tue, 04 Jun 2019 03:52:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-recode-a-categorical-variable/m-p/563433#M157959</guid>
      <dc:creator>SSin</dc:creator>
      <dc:date>2019-06-04T03:52:43Z</dc:date>
    </item>
    <item>
      <title>Re: How to recode a categorical variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-recode-a-categorical-variable/m-p/563434#M157960</link>
      <description>I used If/then. It is like that If/then statement totally does not work. Normally, if and then suppose to go blue if the statement works. But they remains faded and when I run it nothing happen. Do you have any idea why it is not working at all? I am using SAS university edition for the first time.</description>
      <pubDate>Tue, 04 Jun 2019 03:54:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-recode-a-categorical-variable/m-p/563434#M157960</guid>
      <dc:creator>SSin</dc:creator>
      <dc:date>2019-06-04T03:54:12Z</dc:date>
    </item>
    <item>
      <title>Re: How to recode a categorical variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-recode-a-categorical-variable/m-p/563528#M157995</link>
      <description>&lt;P&gt;I'm squinting, but I still can't see your program.&amp;nbsp; If you post the entire DATA step that you tried, I would be happy to respond.&lt;/P&gt;</description>
      <pubDate>Tue, 04 Jun 2019 14:09:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-recode-a-categorical-variable/m-p/563528#M157995</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2019-06-04T14:09:02Z</dc:date>
    </item>
  </channel>
</rss>

