<?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: substr on character and numeric variables to create new variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/substr-on-character-and-numeric-variables-to-create-new-variable/m-p/482211#M124858</link>
    <description>&lt;P&gt;Perhaps not a complete solution, but a few ideas that you need to implement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;First, you can see you don't get all the characters in "Yes".&amp;nbsp; You need to add this before assigning STATUS1 a value:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;length status1 $ 3;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Second, to left-hand justify the digits, you are close but have to add another function:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;status_c = left(put(status, 8.));&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You still have the basic problem to solve about having both numeric and character values.&amp;nbsp; You need to use a character variable in that case.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Finally, you can simplify the comparisons by adding a colon.&amp;nbsp; For example,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;if status in &lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;:&lt;/STRONG&gt;&lt;/FONT&gt; ("178", "b55", "N6", "2225") then status1="Yes";&lt;/P&gt;</description>
    <pubDate>Sat, 28 Jul 2018 21:52:45 GMT</pubDate>
    <dc:creator>Astounding</dc:creator>
    <dc:date>2018-07-28T21:52:45Z</dc:date>
    <item>
      <title>substr on character and numeric variables to create new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/substr-on-character-and-numeric-variables-to-create-new-variable/m-p/482208#M124857</link>
      <description>&lt;P&gt;Hi Everyone;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am trying to create a new variable based on the values of another variable using substr function. My original dataset looks like this:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data try;
input ID status /* add $ to see second output*/ ;
datalines;
1 178
2 178
3 .
4 .
5 B55
6 B55
8 22253
9 22253
10 N65
11 N66
12 22256
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I am using the following function to create the new variable. This function&amp;nbsp;gives the expected results only &amp;nbsp;when I input the status variable in character format ( $ ). My original dataset has status as numerical variable and I have tried too many formats and its not working.&amp;nbsp; The output I get status as numeric and as character are shown below&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set try; 
status1 = 'No';
if   substr(status,1,3) in ("178","b55")
				OR substr(status,1,2) in ("N6") 
                OR substr(status,1,4) in ("2225") 
               then status1='Yes';
			
			       run;
				   proc print data= want ;run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;ObsID status status11234567891011&lt;/P&gt;&lt;TABLE cellspacing="0" cellpadding="5"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;178&lt;/TD&gt;&lt;TD&gt;No&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;178&lt;/TD&gt;&lt;TD&gt;No&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;TD&gt;No&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&g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;any ideas&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 28 Jul 2018 21:44:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/substr-on-character-and-numeric-variables-to-create-new-variable/m-p/482208#M124857</guid>
      <dc:creator>Jordani</dc:creator>
      <dc:date>2018-07-28T21:44:56Z</dc:date>
    </item>
    <item>
      <title>Re: substr on character and numeric variables to create new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/substr-on-character-and-numeric-variables-to-create-new-variable/m-p/482211#M124858</link>
      <description>&lt;P&gt;Perhaps not a complete solution, but a few ideas that you need to implement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;First, you can see you don't get all the characters in "Yes".&amp;nbsp; You need to add this before assigning STATUS1 a value:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;length status1 $ 3;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Second, to left-hand justify the digits, you are close but have to add another function:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;status_c = left(put(status, 8.));&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You still have the basic problem to solve about having both numeric and character values.&amp;nbsp; You need to use a character variable in that case.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Finally, you can simplify the comparisons by adding a colon.&amp;nbsp; For example,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;if status in &lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;:&lt;/STRONG&gt;&lt;/FONT&gt; ("178", "b55", "N6", "2225") then status1="Yes";&lt;/P&gt;</description>
      <pubDate>Sat, 28 Jul 2018 21:52:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/substr-on-character-and-numeric-variables-to-create-new-variable/m-p/482211#M124858</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-07-28T21:52:45Z</dc:date>
    </item>
    <item>
      <title>Re: substr on character and numeric variables to create new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/substr-on-character-and-numeric-variables-to-create-new-variable/m-p/482337#M124912</link>
      <description>&lt;P&gt;1.&lt;EM&gt; My original dataset has status as numerical variable&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&lt;LI-WRAPPER&gt;&lt;/LI-WRAPPER&gt;&lt;/P&gt;
&lt;P&gt;I am confused. How do you get a value of B55 if your variable is numeric?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2. This&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;status_c = put(status, 8.);&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;should probably be left justified:&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;status_c = put(status, 8. -l);&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 30 Jul 2018 03:03:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/substr-on-character-and-numeric-variables-to-create-new-variable/m-p/482337#M124912</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2018-07-30T03:03:02Z</dc:date>
    </item>
    <item>
      <title>Re: substr on character and numeric variables to create new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/substr-on-character-and-numeric-variables-to-create-new-variable/m-p/483504#M125371</link>
      <description>&lt;P&gt;Hi ChrisNZ ,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am importing my dataset from R. My last resort was to copy and paste the data into a data step .&lt;/P&gt;</description>
      <pubDate>Thu, 02 Aug 2018 17:59:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/substr-on-character-and-numeric-variables-to-create-new-variable/m-p/483504#M125371</guid>
      <dc:creator>Jordani</dc:creator>
      <dc:date>2018-08-02T17:59:06Z</dc:date>
    </item>
    <item>
      <title>Re: substr on character and numeric variables to create new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/substr-on-character-and-numeric-variables-to-create-new-variable/m-p/483601#M125419</link>
      <description>&lt;P&gt;Have you seen &lt;A href="https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Can-I-import-a-large-file-from-R-currently-saved-in-RData-format/td-p/141506" target="_self"&gt;this&lt;/A&gt;?&lt;/P&gt;</description>
      <pubDate>Thu, 02 Aug 2018 21:38:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/substr-on-character-and-numeric-variables-to-create-new-variable/m-p/483601#M125419</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2018-08-02T21:38:50Z</dc:date>
    </item>
  </channel>
</rss>

