<?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: Creating new numeric variable based on values of character variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Creating-new-numeric-variable-based-on-values-of-character/m-p/515958#M139309</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/246658"&gt;@Ronin&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your DATA steps are syntactically correct and, somewhat ironically, even the one with the typo in the variable name&amp;nbsp;would produce correct results (!) &lt;EM&gt;if&lt;/EM&gt; the input dataset met certain&amp;nbsp;plausible assumptions.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, these assumptions must be violated to obtain the odd PROC PRINT output you posted.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;SAS programmers around the globe are now curiously waiting for you to post (using the {i} button) the PROC FREQ output that&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954"&gt;@Astounding&lt;/a&gt;&amp;nbsp;asked you to produce. Or, if you're more comfortable with PROC PRINT, please show us the output of this step (two observations):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc print data=lib.mydata2(firstobs=30 obs=31);
format _character_ $hex24.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then I'm sure the issue will be resolved very soon.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 26 Nov 2018 12:22:50 GMT</pubDate>
    <dc:creator>FreelanceReinh</dc:creator>
    <dc:date>2018-11-26T12:22:50Z</dc:date>
    <item>
      <title>Creating new numeric variable based on values of character variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-new-numeric-variable-based-on-values-of-character/m-p/515781#M139234</link>
      <description>&lt;P&gt;Hi,&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am a complete novice to SAS and programming. I have used a set of code to create new variable in a dataset based on value of other variable in the same dataset and it worked:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;libname lib "Mylib";&lt;BR /&gt;data lib.mydata;&lt;BR /&gt;set lib.mydata;&lt;BR /&gt;if WEEK&amp;lt;=1968 then GRP=1;&lt;BR /&gt;else GRP=2;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;proc print data=lib.mydata;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;here, based on the WEEK variable (numeric) values I have created a new numeric variable GRP with only two values 1 or 2&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This actually worked and gave me desired column accurately. But when I tried to apply similar logic in another dataset its giving different output:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;libname lib "mylib";&lt;BR /&gt;data lib.mydata2;&lt;BR /&gt;set lib.mydata2;&lt;BR /&gt;if VAR2="Tier 1" then VAR3=1;&lt;BR /&gt;else if VAR2="Tier 2" then VAR3=2;&lt;BR /&gt;else VAR3=3;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;proc print data=lib.&lt;SPAN&gt;mydata2&lt;/SPAN&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I even tried by changing this code to:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;libname lib "mylib";&lt;BR /&gt;data lib.mydata2;&lt;BR /&gt;set lib.mydata2;&lt;BR /&gt;if VAR2="Tier 1" then VAR3=1;&lt;BR /&gt;else if VAR2="Tier 2" then VAR3=2;&lt;BR /&gt;else if VAR3= "Control" then VAR3=3;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc print data=lib.mydata2;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here I am trying to create VAR3(1,2,3) a numeric variable based on the the character variable VAR2 (Tier 1, Tier 2 and Control). Such that:&lt;/P&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;VAR2&lt;/TD&gt;&lt;TD&gt;VAR3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Tier 1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Tier 2&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Control&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;But the output is taking VAR3=3 for all the observations and the values for VAR3 shifted in the output to the next row starting for each observation&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The output of this&amp;nbsp;code&amp;nbsp;(.lst file) looks like the the data available in the text file attached here.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please have a look and guide me why this is not working. Kindly help me with right code.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 25 Nov 2018 14:58:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-new-numeric-variable-based-on-values-of-character/m-p/515781#M139234</guid>
      <dc:creator>Ronin</dc:creator>
      <dc:date>2018-11-25T14:58:15Z</dc:date>
    </item>
    <item>
      <title>Re: Creating new numeric variable based on values of character variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-new-numeric-variable-based-on-values-of-character/m-p/515782#M139235</link>
      <description>&lt;P&gt;First&lt;BR /&gt;else if VAR2= "Control" then VAR3=3;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;not&lt;/P&gt;
&lt;P&gt;else if VAR3 = "Control" then VAR3=3;&lt;/P&gt;</description>
      <pubDate>Sun, 25 Nov 2018 15:08:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-new-numeric-variable-based-on-values-of-character/m-p/515782#M139235</guid>
      <dc:creator>VDD</dc:creator>
      <dc:date>2018-11-25T15:08:54Z</dc:date>
    </item>
    <item>
      <title>Re: Creating new numeric variable based on values of character variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-new-numeric-variable-based-on-values-of-character/m-p/515798#M139240</link>
      <description>else if VAR2= "Control" then VAR3=3;&lt;BR /&gt;Yes that is what I have used in the code....My mistake for typo error here in asking question&lt;BR /&gt;</description>
      <pubDate>Sun, 25 Nov 2018 17:24:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-new-numeric-variable-based-on-values-of-character/m-p/515798#M139240</guid>
      <dc:creator>Ronin</dc:creator>
      <dc:date>2018-11-25T17:24:05Z</dc:date>
    </item>
    <item>
      <title>Re: Creating new numeric variable based on values of character variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-new-numeric-variable-based-on-values-of-character/m-p/515803#M139242</link>
      <description>&lt;P&gt;Writing over your input dataset is a big source of confusion, especially for novice users.&amp;nbsp; Doing that makes it impossible to change your logic and re-run the program because the input data is now modified.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Not sure what you are trying to show but the text file you posted (why did you post a text file instead of just posting the text into the body of the question using the Insert Code icon?).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Since you are overwriting your inputs probable what happened is that you ran it once and set VAR3 to 3 for every observation. Now when you run the code you posted non of the IF conditions are met so VAR3 keeps it current value of 3.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Make sure that the values of VAR2 actually match the values in your IF conditions. Is the case the same? Do the values in the dataset contain leading spaces?&amp;nbsp; Or other invisible characters?&lt;/P&gt;</description>
      <pubDate>Sun, 25 Nov 2018 17:44:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-new-numeric-variable-based-on-values-of-character/m-p/515803#M139242</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-11-25T17:44:42Z</dc:date>
    </item>
    <item>
      <title>Re: Creating new numeric variable based on values of character variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-new-numeric-variable-based-on-values-of-character/m-p/515882#M139270</link>
      <description>&lt;P&gt;One of the mysteries you will need to solve is what is actually contained in VAR2?&amp;nbsp; It may contain characters that you are not accounting for.&amp;nbsp; Some of the many possibilities:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Perhaps it contains leading blanks&lt;/LI&gt;
&lt;LI&gt;Perhaps it contains all uppercase or all lowercase letters&lt;/LI&gt;
&lt;LI&gt;Perhaps it contains a strange character such as a tab character after the word "Tier"&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;Here's a quick way to verify.&amp;nbsp; Begin with:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data _null_;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;tier_1_in_hex = put("Tier 1", $hex12.);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;tier_2_in_hex = put("Tier 2", $hex12.);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;tier_3_in_hex = put("Tier 3", $hex12.);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;put 'Tier 1 should look like this:&amp;nbsp; '&amp;nbsp; tier_1_in_hex;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;put 'Tier 2 should look like this:&amp;nbsp; '&amp;nbsp; tier_2_in_hex;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;put 'Tier 3 should look like this:&amp;nbsp; '&amp;nbsp; tier_3_in_hex;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That tells you what the characters you expect would look like, if expressed in hex format.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That get a table of what is actually there, also in hex format:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc freq data=lib.mydata;&lt;/P&gt;
&lt;P&gt;tables var2;&lt;/P&gt;
&lt;P&gt;format var2 $hex12.;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Compare the results to verify whether they are the same or not.&amp;nbsp; Let us know what you find.&lt;/P&gt;</description>
      <pubDate>Mon, 26 Nov 2018 03:15:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-new-numeric-variable-based-on-values-of-character/m-p/515882#M139270</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-11-26T03:15:53Z</dc:date>
    </item>
    <item>
      <title>Re: Creating new numeric variable based on values of character variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-new-numeric-variable-based-on-values-of-character/m-p/515894#M139277</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;There might be leading/trailing space for var2 values, so why you are getting all values as var3 = 3,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;try running this code, this might help,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;libname lib "mylib";&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;data lib.mydata2;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;set lib.mydata2;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;if&amp;nbsp; strip(upcase(VAR2))="TIER 1" then VAR3=1;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;else if&amp;nbsp; strip(upcase(VAR2))="TIER 2" then VAR3=2;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;else var3 = 3;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;run;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;regards&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;manoj.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 26 Nov 2018 05:25:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-new-numeric-variable-based-on-values-of-character/m-p/515894#M139277</guid>
      <dc:creator>s_manoj</dc:creator>
      <dc:date>2018-11-26T05:25:08Z</dc:date>
    </item>
    <item>
      <title>Re: Creating new numeric variable based on values of character variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-new-numeric-variable-based-on-values-of-character/m-p/515921#M139292</link>
      <description>I tried that...and checked manually in the dataset csv file no ending and trailing spaces..it din't worked&lt;BR /&gt;</description>
      <pubDate>Mon, 26 Nov 2018 08:51:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-new-numeric-variable-based-on-values-of-character/m-p/515921#M139292</guid>
      <dc:creator>Ronin</dc:creator>
      <dc:date>2018-11-26T08:51:50Z</dc:date>
    </item>
    <item>
      <title>Re: Creating new numeric variable based on values of character variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-new-numeric-variable-based-on-values-of-character/m-p/515945#M139300</link>
      <description>I have check no case of leading and trailing spaces in VAR2 values</description>
      <pubDate>Mon, 26 Nov 2018 10:51:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-new-numeric-variable-based-on-values-of-character/m-p/515945#M139300</guid>
      <dc:creator>Ronin</dc:creator>
      <dc:date>2018-11-26T10:51:23Z</dc:date>
    </item>
    <item>
      <title>Re: Creating new numeric variable based on values of character variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-new-numeric-variable-based-on-values-of-character/m-p/515958#M139309</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/246658"&gt;@Ronin&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your DATA steps are syntactically correct and, somewhat ironically, even the one with the typo in the variable name&amp;nbsp;would produce correct results (!) &lt;EM&gt;if&lt;/EM&gt; the input dataset met certain&amp;nbsp;plausible assumptions.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, these assumptions must be violated to obtain the odd PROC PRINT output you posted.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;SAS programmers around the globe are now curiously waiting for you to post (using the {i} button) the PROC FREQ output that&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954"&gt;@Astounding&lt;/a&gt;&amp;nbsp;asked you to produce. Or, if you're more comfortable with PROC PRINT, please show us the output of this step (two observations):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc print data=lib.mydata2(firstobs=30 obs=31);
format _character_ $hex24.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then I'm sure the issue will be resolved very soon.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 26 Nov 2018 12:22:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-new-numeric-variable-based-on-values-of-character/m-p/515958#M139309</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2018-11-26T12:22:50Z</dc:date>
    </item>
    <item>
      <title>Re: Creating new numeric variable based on values of character variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-new-numeric-variable-based-on-values-of-character/m-p/516064#M139355</link>
      <description>&lt;P&gt;As long as you haven't actually done the work yet, here is a preliminary step you can take to make sure we are pursuing the right path here.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data temp;&lt;/P&gt;
&lt;P&gt;set have;&lt;/P&gt;
&lt;P&gt;len = length(var2);&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc freq data=temp;&lt;/P&gt;
&lt;P&gt;tables var2 * len / missing list;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This step will confirm whether the problem lies in the data or was introduced by many failed experiments in trying to get the program to work.&lt;/P&gt;</description>
      <pubDate>Mon, 26 Nov 2018 17:33:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-new-numeric-variable-based-on-values-of-character/m-p/516064#M139355</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-11-26T17:33:35Z</dc:date>
    </item>
  </channel>
</rss>

