<?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 update a num. var to char. var with add value based on another col in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/how-to-update-a-num-var-to-char-var-with-add-value-based-on/m-p/891643#M352230</link>
    <description>&lt;P&gt;Create a format which displays missing as NE:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format;
value mynum
  . = "NE"
  other = [2.]
;
run;

proc datasets lib=work;
modify have;
format e4 mynum.;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 30 Aug 2023 07:23:58 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2023-08-30T07:23:58Z</dc:date>
    <item>
      <title>how to update a num. var to char. var with add value based on another col</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-update-a-num-var-to-char-var-with-add-value-based-on/m-p/891626#M352219</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a sample data like below:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="stataq_0-1693361831916.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/87294i2DC5827F6D9D306F/image-size/medium?v=v2&amp;amp;px=400" role="button" title="stataq_0-1693361831916.png" alt="stataq_0-1693361831916.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;```&lt;/P&gt;&lt;P&gt;DATALINES;&lt;BR /&gt;Alexander Smith 78 82 86 69 0&lt;BR /&gt;John Simon 88 72 86 . 1&lt;BR /&gt;Patricia Jones 98 92 92 99 0&lt;BR /&gt;Jack Benedict 54 63 71 49 0&lt;BR /&gt;Rene Porter 100 62 88 . 1&lt;BR /&gt;;&lt;BR /&gt;```&lt;/P&gt;&lt;P&gt;I would like to do following task:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1. build about dataset; e1:ab are num. vars.&lt;/P&gt;&lt;P&gt;2. when ab=1, replace e4 value to "NE".&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As e4 currently is num., I think I need to change it to char. and then use if... then to change the value. However when I try that , i got "&lt;/P&gt;&lt;PRE&gt;NOTE: Numeric values have been converted to character values at the places given by: (Line):(Column)."&lt;BR /&gt;&lt;BR /&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;Could anyone give me an example how to correctly do this?&lt;/P&gt;&lt;P&gt;My current code is:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;```&lt;/P&gt;&lt;P&gt;data grades2;&lt;/P&gt;&lt;P&gt;set grades;&lt;/P&gt;&lt;P&gt;e4=put(e4, 8.);&lt;/P&gt;&lt;P&gt;if ab= 1 then e4="NE";&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;```&lt;/P&gt;</description>
      <pubDate>Wed, 30 Aug 2023 02:20:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-update-a-num-var-to-char-var-with-add-value-based-on/m-p/891626#M352219</guid>
      <dc:creator>stataq</dc:creator>
      <dc:date>2023-08-30T02:20:18Z</dc:date>
    </item>
    <item>
      <title>Re: how to update a num. var to char. var with add value based on another col</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-update-a-num-var-to-char-var-with-add-value-based-on/m-p/891627#M352220</link>
      <description>&lt;P&gt;You can't change a variable's type.&amp;nbsp; If &lt;EM&gt;&lt;STRONG&gt;e4&lt;/STRONG&gt;&lt;/EM&gt; is numeric due to the &lt;EM&gt;&lt;STRONG&gt;SET GRADES&lt;/STRONG&gt;&lt;/EM&gt; statement, it can't be made into a character variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, you could change the name of the incoming e4 variable, which means you can then create a new e4&lt;EM&gt;&amp;nbsp;&lt;/EM&gt;variable of whatever type you want.&amp;nbsp; Replace your&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;set grades;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;statement with something like&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;set grades (rename=(e4=_e4));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;then calculate the new e4 using the values in variable _e4.&lt;/P&gt;</description>
      <pubDate>Wed, 30 Aug 2023 03:07:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-update-a-num-var-to-char-var-with-add-value-based-on/m-p/891627#M352220</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2023-08-30T03:07:43Z</dc:date>
    </item>
    <item>
      <title>Re: how to update a num. var to char. var with add value based on another col</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-update-a-num-var-to-char-var-with-add-value-based-on/m-p/891628#M352221</link>
      <description>&lt;P&gt;Thanks.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;One silly question, is it a easy way to build my sample data.&amp;nbsp; I currently only know how to build one looks like that but all char. &lt;span class="lia-unicode-emoji" title=":grinning_face_with_sweat:"&gt;😅&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 30 Aug 2023 02:36:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-update-a-num-var-to-char-var-with-add-value-based-on/m-p/891628#M352221</guid>
      <dc:creator>stataq</dc:creator>
      <dc:date>2023-08-30T02:36:34Z</dc:date>
    </item>
    <item>
      <title>Re: how to update a num. var to char. var with add value based on another col</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-update-a-num-var-to-char-var-with-add-value-based-on/m-p/891634#M352225</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/448857"&gt;@stataq&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thanks.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One silly question, is it a easy way to build my sample data.&amp;nbsp; I currently only know how to build one looks&lt;/P&gt;
like that but all char. &lt;span class="lia-unicode-emoji" title=":grinning_face_with_sweat:"&gt;😅&lt;/span&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Do you mean how to read in your example datalines?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATALINES;
Alexander Smith 78 82 86 69 0
John Simon 88 72 86 . 1
Patricia Jones 98 92 92 99 0
Jack Benedict 54 63 71 49 0
Rene Porter 100 62 88 . 1
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In general you should DEFINE the variables using a LENGTH statement (or possibly the ATTRIB statement with the LENGTH= option).&amp;nbsp; So if the first two variables are first name and last name then perhaps you should set them to be character of length 20 each. The other 5 variables look like numbers so just set the length to 8 (SAS stores all numbers as 64-bit floating point values, which require 8 bytes to store.)&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;length fname $20 lname $20 x1 x2 x3 x4 x5 8 ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then when you get to writing the INPUT statement you can just list the variable's names.&amp;nbsp; Or even use a variable list.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  length fname $20 lname $20 x1-x5 8 ;
  input fname -- x5;
DATALINES;
Alexander Smith 78 82 86 69 0
John Simon 88 72 86 . 1
Patricia Jones 98 92 92 99 0
Jack Benedict 54 63 71 49 0
Rene Porter 100 62 88 . 1
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note those particular lines will work fine since every names is exactly two words long.&amp;nbsp; But what if you had&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Billy Ray Smith 78 82 86 69 0&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;instead?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If the values of one of the variables contain spaces you might want to use something other than space as the delimiter between the values.&amp;nbsp; So perhaps the | character.&amp;nbsp; To tell the data step what delimiter it should recognize you need an INFILE statement.&amp;nbsp; You can use one for in-line data, just use DATALINES (or its alias CARDS) as the fileref.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  infile datalines dsd dlm='|' ;
  length fname $20 lname $20 x1-x5 8 ;
  input fname -- x5;
DATALINES;
Alexander|Smith|78|82|86|69|0
Billy Ray|Smith|78|82|86|69|0
John|Simon|88|72|86|.|1
Patricia|Jones|98|92|92|99|0
Jack|Benedict|54|63|71|49|0
Rene|Porter|100|62|88|.|1
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 30 Aug 2023 04:23:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-update-a-num-var-to-char-var-with-add-value-based-on/m-p/891634#M352225</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-08-30T04:23:07Z</dc:date>
    </item>
    <item>
      <title>Re: how to update a num. var to char. var with add value based on another col</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-update-a-num-var-to-char-var-with-add-value-based-on/m-p/891643#M352230</link>
      <description>&lt;P&gt;Create a format which displays missing as NE:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format;
value mynum
  . = "NE"
  other = [2.]
;
run;

proc datasets lib=work;
modify have;
format e4 mynum.;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 30 Aug 2023 07:23:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-update-a-num-var-to-char-var-with-add-value-based-on/m-p/891643#M352230</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-08-30T07:23:58Z</dc:date>
    </item>
  </channel>
</rss>

