<?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: Changing character sex variable to numeric in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Changing-character-sex-variable-to-numeric/m-p/917502#M41062</link>
    <description>&lt;P&gt;As Tom point out , you should use INFORMAT , not FORMAT to change character into numeric .&lt;/P&gt;
&lt;P&gt;And If you do not care about upper or lower case, you could add one more option UPCASE.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format;
invalue gender(upcase)
  'MALE'   = 1
  'FEMALE' = 2
;
run;

data want;
  set have;
  sex = input(gender,gender.);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 23 Feb 2024 01:56:03 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2024-02-23T01:56:03Z</dc:date>
    <item>
      <title>Changing character sex variable to numeric</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Changing-character-sex-variable-to-numeric/m-p/917447#M41047</link>
      <description>&lt;P&gt;I am trying to change the gender(renamed sex) variable from "female" and "male" to "2" and "1". I am trying to achieve this using proc format but keep receiving the error "&amp;nbsp;1474 'male' = 1;&lt;BR /&gt;NOTE: Format $GENDER is already on the library WORK.FORMATS.&lt;BR /&gt;NOTE: Format $GENDER has been output.&lt;BR /&gt;1475 'female' = 2;&lt;BR /&gt;--------&lt;BR /&gt;180&lt;BR /&gt;ERROR 180-322: Statement is not valid or it is used out of proper order."&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is the sample of my code:&lt;/P&gt;&lt;P&gt;proc format;&lt;BR /&gt;value $gender&lt;BR /&gt;'male' = 1;&lt;BR /&gt;'female' = 2;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;data PUF16;&lt;BR /&gt;set PUF2016;&lt;BR /&gt;keep inc_key age gender race1;&lt;BR /&gt;rename inc_key = patientID;&lt;BR /&gt;rename age = ageyears;&lt;BR /&gt;rename gender = sex;&lt;BR /&gt;rename race1 = race;&lt;BR /&gt;format sex $sex.;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is there a "more permanent way to change the variable or is proc format the only way?&lt;/P&gt;</description>
      <pubDate>Thu, 22 Feb 2024 20:54:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Changing-character-sex-variable-to-numeric/m-p/917447#M41047</guid>
      <dc:creator>PunkinSAS08</dc:creator>
      <dc:date>2024-02-22T20:54:05Z</dc:date>
    </item>
    <item>
      <title>Re: Changing character sex variable to numeric</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Changing-character-sex-variable-to-numeric/m-p/917448#M41048</link>
      <description>&lt;P&gt;You cannot change a variable's type.&amp;nbsp; You can make a NEW variable if you want.&amp;nbsp; You could then change the names of the variables so the new variable uses the same name as the old variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;PROC FORMAT has nothing to do with changing a variable's type.&amp;nbsp; In fact the actions of PROC FORMAT have nothing to do with variables at all.&amp;nbsp; They are for defining FORMATs and INFORMATs.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A FORMAT is special instructions on how to convert values into text.&amp;nbsp; An INFORMAT is special instructions for how to convert text into values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A character FORMAT converts text values to text.&amp;nbsp; A numeric FORMAT converts numeric values to text.&amp;nbsp; A numeric INFORMAT converts text to a number.&amp;nbsp; A character INFORMAT converts text to other text.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The only way to use either of those to help with converting strings like 'male' into numbers like 1 would be to make a numeric INFORMAT.&amp;nbsp; &amp;nbsp;Your code is making a character FORMAT.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And then you don't even TRY to use what you made to CONVERT the variable.&amp;nbsp; All you appear to do is try to ATTACH the format to a variable so that it will be DISPLAYED in a different way.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To use an INFORMAT() to make a numeric value from a character string you need to use the INPUT() function or the INPUT statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So if your existing variable is named GENDER and has values of 'male' or 'female' you could do something like this to make a new numeric variable named SEX that has values of 1 or 2.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format;
invalue gender
  'male' = 1;
  'female' = 2
;
run;

data want;
  set have;
  sex = input(gender,gender.);
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 22 Feb 2024 21:10:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Changing-character-sex-variable-to-numeric/m-p/917448#M41048</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-02-22T21:10:19Z</dc:date>
    </item>
    <item>
      <title>Re: Changing character sex variable to numeric</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Changing-character-sex-variable-to-numeric/m-p/917502#M41062</link>
      <description>&lt;P&gt;As Tom point out , you should use INFORMAT , not FORMAT to change character into numeric .&lt;/P&gt;
&lt;P&gt;And If you do not care about upper or lower case, you could add one more option UPCASE.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format;
invalue gender(upcase)
  'MALE'   = 1
  'FEMALE' = 2
;
run;

data want;
  set have;
  sex = input(gender,gender.);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 23 Feb 2024 01:56:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Changing-character-sex-variable-to-numeric/m-p/917502#M41062</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2024-02-23T01:56:03Z</dc:date>
    </item>
    <item>
      <title>Re: Changing character sex variable to numeric</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Changing-character-sex-variable-to-numeric/m-p/917504#M41063</link>
      <description>&lt;P&gt;No need for a special format.&amp;nbsp; A sample array of "lookup" values in an array works fine:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  array gender_list {2} $6  _temporary_ ('Male','Female');
  sex=whichc(gender,of gender_list{*});
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 23 Feb 2024 02:19:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Changing-character-sex-variable-to-numeric/m-p/917504#M41063</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2024-02-23T02:19:11Z</dc:date>
    </item>
    <item>
      <title>Re: Changing character sex variable to numeric</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Changing-character-sex-variable-to-numeric/m-p/917533#M41068</link>
      <description>&lt;P&gt;The reason for your ERROR:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format;
value $gender
'male' = 1; /* this semicolon terminates the VALUE statement */
'female' = 2; /* therefore this turns into invalid syntax */
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 23 Feb 2024 07:02:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Changing-character-sex-variable-to-numeric/m-p/917533#M41068</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2024-02-23T07:02:33Z</dc:date>
    </item>
  </channel>
</rss>

