<?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 Struggling with sorting categorical data in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Struggling-with-sorting-categorical-data/m-p/861644#M82546</link>
    <description>&lt;P&gt;I am trying to sort data that has 4 possible categorical data options: Independent, Republican, Democrat, (blank).&lt;/P&gt;&lt;P&gt;I am trying to make a confidence interval, but first I need to have the categorical variables be numeric characters instead, so I tried to sort them using:&lt;/P&gt;&lt;P&gt;data=gss08_Q1;&lt;BR /&gt;&amp;nbsp;input=gss08;&lt;BR /&gt;&amp;nbsp; if polparty = 'independent' then polparty1 = 1;&lt;BR /&gt;&amp;nbsp; if polparty = 'rebublican' then polparty1 = 2;&lt;BR /&gt;&amp;nbsp; if polparty = 'democrat' then polparty1= 3;&lt;BR /&gt;&amp;nbsp; else polparty= 4;&lt;/P&gt;&lt;P&gt;This returned with multiple of the same errors, am I using this wrong?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 01 Mar 2023 03:44:15 GMT</pubDate>
    <dc:creator>JSMI1443</dc:creator>
    <dc:date>2023-03-01T03:44:15Z</dc:date>
    <item>
      <title>Struggling with sorting categorical data</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Struggling-with-sorting-categorical-data/m-p/861644#M82546</link>
      <description>&lt;P&gt;I am trying to sort data that has 4 possible categorical data options: Independent, Republican, Democrat, (blank).&lt;/P&gt;&lt;P&gt;I am trying to make a confidence interval, but first I need to have the categorical variables be numeric characters instead, so I tried to sort them using:&lt;/P&gt;&lt;P&gt;data=gss08_Q1;&lt;BR /&gt;&amp;nbsp;input=gss08;&lt;BR /&gt;&amp;nbsp; if polparty = 'independent' then polparty1 = 1;&lt;BR /&gt;&amp;nbsp; if polparty = 'rebublican' then polparty1 = 2;&lt;BR /&gt;&amp;nbsp; if polparty = 'democrat' then polparty1= 3;&lt;BR /&gt;&amp;nbsp; else polparty= 4;&lt;/P&gt;&lt;P&gt;This returned with multiple of the same errors, am I using this wrong?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 01 Mar 2023 03:44:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Struggling-with-sorting-categorical-data/m-p/861644#M82546</guid>
      <dc:creator>JSMI1443</dc:creator>
      <dc:date>2023-03-01T03:44:15Z</dc:date>
    </item>
    <item>
      <title>Re: Struggling with sorting categorical data</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Struggling-with-sorting-categorical-data/m-p/861651#M82547</link>
      <description>&lt;P&gt;You've got other syntax errors in your code like&amp;nbsp;&lt;SPAN&gt;&lt;EM&gt;data&lt;FONT color="#FF0000"&gt;=&lt;/FONT&gt;gss08_Q1&lt;/EM&gt;; but I'm not going to comment on a code snippet with no error log provided.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;Your "core" syntax should work like tested below once you fix the last assignment&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1677646838417.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/80972i7DF8CD8F13D73042/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_0-1677646838417.png" alt="Patrick_0-1677646838417.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Adding a few more ELSE for efficiency wouldn't hurt as well.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
  polparty='independent';
  if polparty = 'independent' then polparty1 = 1;
  ELSE if polparty = 'rebublican' then polparty1 = 2;
  ELSE if polparty = 'democrat' then polparty1= 3;
  else polparty1= 4;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Another approach for recoding a value is using a format/informat. Especially if you've got a lot different values for a category this leads to code that's easier to read.&lt;/P&gt;
&lt;P&gt;In below example using an informat with the upcase option you've got also the advantage that using the format will work on source strings whether upper-, mixed- or lowcase.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format;
  invalue pp_num(upcase)
  'INDEPENDENT' =1
  'REPUBLICAN'  =2
  'DEMOCRAT'    =3
  other         =4
  ;
run;

data test2;
  polparty='independent';
  polparty1=input(polparty,pp_num.);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 01 Mar 2023 05:07:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Struggling-with-sorting-categorical-data/m-p/861651#M82547</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2023-03-01T05:07:16Z</dc:date>
    </item>
  </channel>
</rss>

