<?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: recode character variable with if/then statements in SAS Data Management</title>
    <link>https://communities.sas.com/t5/SAS-Data-Management/recode-character-variable-with-if-then-statements/m-p/543289#M16705</link>
    <description>&lt;P&gt;Hi Tom,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for pointing that out! After I change 6. to 1. and the issue is solved:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data mydata;
set mydata;
film1 = put(film, 1.); *the key is here;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;then&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data mydata;
length filmname &amp;amp; 16.;
set mydata;
if film1= '1' then filmname = 'batman';
else if film1 = '2' then filmname = 'goodwillhunting';
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;img id="smileyhappy" class="emoticon emoticon-smileyhappy" src="https://communities.sas.com/i/smilies/16x16_smiley-happy.png" alt="Smiley Happy" title="Smiley Happy" /&gt;&lt;/P&gt;</description>
    <pubDate>Thu, 14 Mar 2019 19:07:36 GMT</pubDate>
    <dc:creator>shonn2nd</dc:creator>
    <dc:date>2019-03-14T19:07:36Z</dc:date>
    <item>
      <title>recode character variable with if/then statements</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/recode-character-variable-with-if-then-statements/m-p/542996#M16695</link>
      <description>&lt;P&gt;Not sure why if/then statement can't recode a character variable. My code doesn't work. Can anyone hep me? Thanks.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data mydata;
input film $ rating;
datalines;
1 2.8
1 3.8
1 4.4
1 2.9
1 2.3
2 2.7
2 3.8
2 3.3
2 4.0
2 4.2
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data&amp;nbsp;mydata;
set&amp;nbsp;mydata;
if&amp;nbsp;film&amp;nbsp;=&amp;nbsp;"1"&amp;nbsp;then&amp;nbsp;filmname&amp;nbsp;=&amp;nbsp;"batman";
else&amp;nbsp;if&amp;nbsp;film&amp;nbsp;=&amp;nbsp;"2"&amp;nbsp;then&amp;nbsp;filmname&amp;nbsp;=&amp;nbsp;"goodwillhunting";
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 14 Mar 2019 02:34:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/recode-character-variable-with-if-then-statements/m-p/542996#M16695</guid>
      <dc:creator>shonn2nd</dc:creator>
      <dc:date>2019-03-14T02:34:02Z</dc:date>
    </item>
    <item>
      <title>Re: recode character variable with if/then statements</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/recode-character-variable-with-if-then-statements/m-p/542997#M16696</link>
      <description>&lt;P&gt;Try below code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data mydata1;&lt;BR /&gt;length film $16.; /*&amp;nbsp;goodwillhunting exceeds 8 bytes long, so change the column length to 16 */&lt;BR /&gt;set mydata;&lt;BR /&gt;if film = "1" then film = "batman"; /* recode the values for film variable */&lt;BR /&gt;else if film = "2" then film = "goodwillhunting";&lt;BR /&gt;rename film=filmname; /* rename film to filmnale if you would like the variable as filmname */&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Thu, 14 Mar 2019 03:06:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/recode-character-variable-with-if-then-statements/m-p/542997#M16696</guid>
      <dc:creator>sbjwhl</dc:creator>
      <dc:date>2019-03-14T03:06:34Z</dc:date>
    </item>
    <item>
      <title>Re: recode character variable with if/then statements</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/recode-character-variable-with-if-then-statements/m-p/543000#M16697</link>
      <description>&lt;P&gt;The length of FILMNAME is the key issue here.&amp;nbsp; Since "batman" is the first value assigned, it receives a length of $ 6 (not long enough).&amp;nbsp; Try:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data mydata;
   set mydata;
   length filmname $ 15;
   if film = "1" then filmname = "batman";
   else if film = "2" then filmname = "goodwillhunting";
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 14 Mar 2019 03:47:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/recode-character-variable-with-if-then-statements/m-p/543000#M16697</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2019-03-14T03:47:20Z</dc:date>
    </item>
    <item>
      <title>Re: recode character variable with if/then statements</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/recode-character-variable-with-if-then-statements/m-p/543081#M16698</link>
      <description>&lt;P&gt;Using a format for re-coding is the recommended way. The benefits are:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;less code in the data step&lt;/LI&gt;
&lt;LI&gt;the mapping can be read from file, thus you don't have to change code if a misspelling is found&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 14 Mar 2019 10:28:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/recode-character-variable-with-if-then-statements/m-p/543081#M16698</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2019-03-14T10:28:04Z</dc:date>
    </item>
    <item>
      <title>Re: recode character variable with if/then statements</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/recode-character-variable-with-if-then-statements/m-p/543204#M16699</link>
      <description>&lt;P&gt;For many purposes displaying another value for a single variable can be accomplished with a FORMAT and removes the need for adding multiple variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;proc format library=work;
value $filmname
'1'='batman'
'2'='goodwillhunting'
;
run;
data mydata;
input film $ rating;
datalines;
1 2.8
1 3.8
1 4.4
1 2.9
1 2.3
2 2.7
2 3.8
2 3.3
2 4.0
2 4.2
;
run;

proc print data=mydata;
   format film $filmname.;
run;

&lt;/PRE&gt;
&lt;P&gt;And you can get different results by using a different format:&lt;/P&gt;
&lt;PRE&gt;proc format library=work;
value $genre
'1'='Comic adventure'
'2'='Drama'
;

proc print data=mydata;
   format film $genre.;
run;&lt;/PRE&gt;
&lt;P&gt;These formats will be honored by most SAS analysis or graphing procedures for creating groups:&lt;/P&gt;
&lt;PRE&gt;proc means data=mydata;
   class film;
   format film $filmname.;
   var rating;
run;

&lt;/PRE&gt;</description>
      <pubDate>Thu, 14 Mar 2019 15:17:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/recode-character-variable-with-if-then-statements/m-p/543204#M16699</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-03-14T15:17:17Z</dc:date>
    </item>
    <item>
      <title>Re: recode character variable with if/then statements</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/recode-character-variable-with-if-then-statements/m-p/543218#M16700</link>
      <description>&lt;P&gt;Hello sbjwhl,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for the help, but I don't know why It doesn't work. I think I forgot to tell an information here.&lt;/P&gt;&lt;P&gt;Below is the original dataset when all the variables are still numeric:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data mydata;
input film rating; *film is numeric;
datalines;
1 2.8
1 3.8
1 4.4
1 2.9
1 2.3
2 2.7
2 3.8
2 3.3
2 4.0
2 4.2
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;then I convert "film" to character variable by creating film1 variable:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data mydata;
set mydata;
film1 = put(film, 6.);
run; &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;then I started to use the approach you suggested:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data mydata;
length film1 $16.;
set mydata;
if film1 = "1" then film1 = "batman";
else if film1 = "2" then film1 = "goodwillhunting";
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;But it doesn't work. Film1 is still 1 and 2. Do you know why? Thanks.&lt;/P&gt;</description>
      <pubDate>Thu, 14 Mar 2019 15:36:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/recode-character-variable-with-if-then-statements/m-p/543218#M16700</guid>
      <dc:creator>shonn2nd</dc:creator>
      <dc:date>2019-03-14T15:36:00Z</dc:date>
    </item>
    <item>
      <title>Re: recode character variable with if/then statements</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/recode-character-variable-with-if-then-statements/m-p/543221#M16701</link>
      <description>&lt;P&gt;Hello Astounding,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for the help, but I don't know why It doesn't work. I think I forgot to tell an information here.&lt;/P&gt;&lt;P&gt;Below is the original dataset when all the variables are still numeric:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data mydata;
input film rating; *film is numeric;
datalines;
1 2.8
1 3.8
1 4.4
1 2.9
1 2.3
2 2.7
2 3.8
2 3.3
2 4.0
2 4.2
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;then I convert "film" to character variable by creating film1 variable:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data mydata;
set mydata;
film1 = put(film, 6.);
run; &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;then I started to use the approach you suggested:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data mydata;&lt;BR /&gt;set mydata;
length filmname $15;
if film1 = "1" then filmname = "batman";
else if film1 = "2" then filmname = "goodwillhunting";
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;But it doesn't work. filmname is empty. Do you know why? Thanks.&lt;/P&gt;</description>
      <pubDate>Thu, 14 Mar 2019 15:40:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/recode-character-variable-with-if-then-statements/m-p/543221#M16701</guid>
      <dc:creator>shonn2nd</dc:creator>
      <dc:date>2019-03-14T15:40:17Z</dc:date>
    </item>
    <item>
      <title>Re: recode character variable with if/then statements</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/recode-character-variable-with-if-then-statements/m-p/543228#M16702</link>
      <description>&lt;P&gt;I know why, but don't bother fixing it.&amp;nbsp; Just use FILM instead of FILM1:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;if film=1 then filmname = "batman";&lt;/P&gt;</description>
      <pubDate>Thu, 14 Mar 2019 15:53:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/recode-character-variable-with-if-then-statements/m-p/543228#M16702</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2019-03-14T15:53:48Z</dc:date>
    </item>
    <item>
      <title>Re: recode character variable with if/then statements</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/recode-character-variable-with-if-then-statements/m-p/543233#M16703</link>
      <description>&lt;P&gt;Thank you. Do you think the main issue lies in the process of converting numeric variable to character variable? Since when I checked ViewTable, film1 is more like centered instead of right-aligned like other char variables.&lt;/P&gt;</description>
      <pubDate>Thu, 14 Mar 2019 16:05:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/recode-character-variable-with-if-then-statements/m-p/543233#M16703</guid>
      <dc:creator>shonn2nd</dc:creator>
      <dc:date>2019-03-14T16:05:43Z</dc:date>
    </item>
    <item>
      <title>Re: recode character variable with if/then statements</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/recode-character-variable-with-if-then-statements/m-p/543260#M16704</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/91450"&gt;@shonn2nd&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thank you. Do you think the main issue lies in the process of converting numeric variable to character variable? Since when I checked ViewTable, film1 is more like centered instead of right-aligned like other char variables.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Exactly.&amp;nbsp; You told it to convert it to a six character string.&amp;nbsp; So the value 1 was converted to '&amp;nbsp; &amp;nbsp; &amp;nbsp;1' and not '1'.&lt;/P&gt;
&lt;P&gt;You could change how you create the character variable to make it left aligned.&lt;/P&gt;
&lt;P&gt;Examples:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;film1=put(film,6.-L);
film1=left(put(film,6.));&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 14 Mar 2019 17:27:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/recode-character-variable-with-if-then-statements/m-p/543260#M16704</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-03-14T17:27:54Z</dc:date>
    </item>
    <item>
      <title>Re: recode character variable with if/then statements</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/recode-character-variable-with-if-then-statements/m-p/543289#M16705</link>
      <description>&lt;P&gt;Hi Tom,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for pointing that out! After I change 6. to 1. and the issue is solved:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data mydata;
set mydata;
film1 = put(film, 1.); *the key is here;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;then&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data mydata;
length filmname &amp;amp; 16.;
set mydata;
if film1= '1' then filmname = 'batman';
else if film1 = '2' then filmname = 'goodwillhunting';
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;img id="smileyhappy" class="emoticon emoticon-smileyhappy" src="https://communities.sas.com/i/smilies/16x16_smiley-happy.png" alt="Smiley Happy" title="Smiley Happy" /&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 14 Mar 2019 19:07:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/recode-character-variable-with-if-then-statements/m-p/543289#M16705</guid>
      <dc:creator>shonn2nd</dc:creator>
      <dc:date>2019-03-14T19:07:36Z</dc:date>
    </item>
  </channel>
</rss>

