<?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: Converting Character to Numeric and Removing Quotes in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Converting-Character-to-Numeric-and-Removing-Quotes/m-p/334375#M75507</link>
    <description>&lt;P&gt;In your previous, yesterday, post you have attached the sample_population.csv file.&lt;/P&gt;
&lt;P&gt;In my response you got the code to read the file as is, option to rename the population variables and&lt;/P&gt;
&lt;P&gt;convert them to numeric. Have you tried the code ? It is attached again here with slight change to enable easy&lt;/P&gt;
&lt;P&gt;define preffered prefix&amp;nbsp;for population variables:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename pop '/folders/myshortcuts/My_Folders/flat/samplefile-pop.csv'; /* adapt path and name */
%let from_year = 2000;
%let upto_year = 2007;&lt;BR /&gt;%let prefix = pop; /* change prefix to your preffered */
data want;
    infile pop truncover firstobs=2;
    input a_line $120.;
    
    city = scan(a_line,1,',');
    state = scan(a_line,2,',');
    
    pos = index(a_line,strip(state)); 
    pos = pos + lengthn(state) +2; 
    popx = compress(substr(a_line,pos),','); 
    
    array pop &amp;amp;prefix.&amp;amp;from_year - &amp;amp;prefix.&amp;amp;upto_year;
    
    do i=1 to 8;
       vx = scan(popx,i,'"'); put i= vx=;
       pop(i) = input(vx,comma12.);
    end;
    keep city state &amp;amp;prefix.&amp;amp;from_year - &amp;amp;prefix.&amp;amp;upto_year;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 20 Feb 2017 16:22:02 GMT</pubDate>
    <dc:creator>Shmuel</dc:creator>
    <dc:date>2017-02-20T16:22:02Z</dc:date>
    <item>
      <title>Converting Character to Numeric and Removing Quotes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-Character-to-Numeric-and-Removing-Quotes/m-p/334331#M75503</link>
      <description>&lt;P&gt;This is a follow up to an problem I posted yesterday:&amp;nbsp;&lt;A href="https://communities.sas.com/t5/Base-SAS-Programming/Importing-dataset-with-dates-as-column-headers/m-p/334194#U334194" target="_blank"&gt;https://communities.sas.com/t5/Base-SAS-Programming/Importing-dataset-with-dates-as-column-headers/m-p/334194#U334194&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a .csv file with State, County, and population from 2000-2007. See attached sample. &amp;nbsp;I was able to successfully load the file using proc import, however, I did not notice the&amp;nbsp;population data loaded as character values.&amp;nbsp;I started over and decided to import the file by copying and modifying code from the log; however, that's not working either.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data WORK.POPULATION2001_2007 ;&lt;BR /&gt;%let _EFIERR_ = 0; /* set the ERROR detection macro variable */&lt;BR /&gt;infile 'C:\population_by_county_2001_2007.csv' delimiter = ',' MISSOVER DSD lrecl=32767 firstobs=2 ;&lt;BR /&gt;informat State $25. ;&lt;BR /&gt;informat County $30. ;&lt;BR /&gt;informat _1_1_2001 best12. ;&lt;BR /&gt;informat _1_1_2002 best12. ;&lt;BR /&gt;informat _1_1_2003 best12. ;&lt;BR /&gt;informat _1_1_2004 best12. ;&lt;BR /&gt;informat _1_1_2005 best12. ;&lt;BR /&gt;informat _1_1_2006 best12. ;&lt;BR /&gt;informat _1_1_2007 best12. ;&lt;BR /&gt;format State $25. ;&lt;BR /&gt;format County $30. ;&lt;BR /&gt;format _1_1_2001 comma12. ;&lt;BR /&gt;format _1_1_2002 comma12. ;&lt;BR /&gt;format _1_1_2003 comma12. ;&lt;BR /&gt;format _1_1_2004 comma12. ;&lt;BR /&gt;format _1_1_2005 comma12. ;&lt;BR /&gt;format _1_1_2006 comma12. ;&lt;BR /&gt;format _1_1_2007 comma12. ;&lt;BR /&gt;input&lt;BR /&gt;State $&lt;BR /&gt;County $&lt;BR /&gt;_1_1_2001 &amp;nbsp; &amp;nbsp;/*NOTE: I'll change the column header's later using a macro that was suggested in yesterday's post*/&lt;BR /&gt;_1_1_2002&lt;BR /&gt;_1_1_2003&lt;BR /&gt;_1_1_2004&lt;BR /&gt;_1_1_2005&lt;BR /&gt;_1_1_2006&lt;BR /&gt;_1_1_2007&lt;BR /&gt;;&lt;BR /&gt;if _ERROR_ then call symputx('_EFIERR_',1); /* set ERROR detection macro variable */&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here's a sample of the error message I'm getting:&lt;/P&gt;&lt;P&gt;NOTE: Invalid data for _1_1_2007 in line 20 95-100.&lt;BR /&gt;RULE: ----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+---&lt;BR /&gt;20 Alabama,Coosa County,"11,746","11,539","11,423","11,156","10,964","10,857","10,783"&amp;nbsp;102&lt;BR /&gt;State=Alabama County=Coosa County _7_1_2001=. _7_1_2002=. _7_1_2003=. _7_1_2004=.&lt;BR /&gt;_7_1_2005=. _7_1_2006=. _7_1_2007=. &amp;nbsp;_ERROR_=1&amp;nbsp;_N_=19&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For one thing, it appears my data still has quotes around it. &amp;nbsp;I thought having DSD in the infile statement was supposed to help with issues such as these. &amp;nbsp;Any thoughts on what I'm doing wrong?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Also, is there a better way to change data from character to numeric?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am a student who is new to SAS, so while there might be something that's glaring obvious to most, it may not be to me -- at least not yet &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 20 Feb 2017 13:36:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-Character-to-Numeric-and-Removing-Quotes/m-p/334331#M75503</guid>
      <dc:creator>KDS_1113</dc:creator>
      <dc:date>2017-02-20T13:36:56Z</dc:date>
    </item>
    <item>
      <title>Re: Converting Character to Numeric and Removing Quotes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-Character-to-Numeric-and-Removing-Quotes/m-p/334332#M75504</link>
      <description>&lt;P&gt;You should use the comma informat for your numeric variables.&lt;/P&gt;</description>
      <pubDate>Mon, 20 Feb 2017 13:50:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-Character-to-Numeric-and-Removing-Quotes/m-p/334332#M75504</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-02-20T13:50:34Z</dc:date>
    </item>
    <item>
      <title>Re: Converting Character to Numeric and Removing Quotes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-Character-to-Numeric-and-Removing-Quotes/m-p/334339#M75505</link>
      <description>&lt;P&gt;What you see in the line here:&lt;/P&gt;
&lt;PRE&gt;RULE: ----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+---
20 Alabama,Coosa County,"11,746","11,539","11,423","11,156","10,964","10,857","10,783"&lt;/PRE&gt;
&lt;P&gt;is not your SAS data, but the line from the infile "as is", so you will see the double quotes as they are delivered.&lt;/P&gt;
&lt;P&gt;And the line for Broome county in your example text file misses the closing double quote for _01_01_2007.&lt;/P&gt;
&lt;P&gt;This program:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
infile "$HOME/sascommunity/population_by_county_2001_2007.txt" dlm=',' dsd truncover firstobs=2;
informat
  State $25.
  County $30.
  _1_1_2001
  _1_1_2002
  _1_1_2003
  _1_1_2004
  _1_1_2005
  _1_1_2006
  _1_1_2007
    comma12.
;
input
  State
  County
  _1_1_2001
  _1_1_2002
  _1_1_2003
  _1_1_2004
  _1_1_2005
  _1_1_2006
  _1_1_2007
;
run;

proc print data=test;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;delivered this result:&lt;/P&gt;
&lt;PRE&gt;                               _1_1_     _1_1_     _1_1_     _1_1_     _1_1_     _1_1_     _1_1_
Obs     State      County      2001      2002      2003      2004      2005      2006      2007

 1     New York    Albany     295378    296644    298491    298819    298605    299033    298246
 2     New York    Broome     199999    199714    198364    197463    196103    195700         .
 3     New York    Chemung     90673     90409     89816     89366     88351     88164     88058
&lt;/PRE&gt;</description>
      <pubDate>Mon, 20 Feb 2017 14:01:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-Character-to-Numeric-and-Removing-Quotes/m-p/334339#M75505</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-02-20T14:01:00Z</dc:date>
    </item>
    <item>
      <title>Re: Converting Character to Numeric and Removing Quotes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-Character-to-Numeric-and-Removing-Quotes/m-p/334375#M75507</link>
      <description>&lt;P&gt;In your previous, yesterday, post you have attached the sample_population.csv file.&lt;/P&gt;
&lt;P&gt;In my response you got the code to read the file as is, option to rename the population variables and&lt;/P&gt;
&lt;P&gt;convert them to numeric. Have you tried the code ? It is attached again here with slight change to enable easy&lt;/P&gt;
&lt;P&gt;define preffered prefix&amp;nbsp;for population variables:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename pop '/folders/myshortcuts/My_Folders/flat/samplefile-pop.csv'; /* adapt path and name */
%let from_year = 2000;
%let upto_year = 2007;&lt;BR /&gt;%let prefix = pop; /* change prefix to your preffered */
data want;
    infile pop truncover firstobs=2;
    input a_line $120.;
    
    city = scan(a_line,1,',');
    state = scan(a_line,2,',');
    
    pos = index(a_line,strip(state)); 
    pos = pos + lengthn(state) +2; 
    popx = compress(substr(a_line,pos),','); 
    
    array pop &amp;amp;prefix.&amp;amp;from_year - &amp;amp;prefix.&amp;amp;upto_year;
    
    do i=1 to 8;
       vx = scan(popx,i,'"'); put i= vx=;
       pop(i) = input(vx,comma12.);
    end;
    keep city state &amp;amp;prefix.&amp;amp;from_year - &amp;amp;prefix.&amp;amp;upto_year;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 20 Feb 2017 16:22:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-Character-to-Numeric-and-Removing-Quotes/m-p/334375#M75507</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2017-02-20T16:22:02Z</dc:date>
    </item>
    <item>
      <title>Re: Converting Character to Numeric and Removing Quotes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-Character-to-Numeric-and-Removing-Quotes/m-p/334498#M75542</link>
      <description>&lt;P&gt;Shmuel, thank you. I had not tried the code. &amp;nbsp;I only started to learn about marcro's last week, so I'm not familiar with a_line, trucover, and other items in the code (i.e., vx).&amp;nbsp; If you have the time, and don't mind, would you kindly explain what they accomplish?&lt;/P&gt;</description>
      <pubDate>Mon, 20 Feb 2017 22:58:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-Character-to-Numeric-and-Removing-Quotes/m-p/334498#M75542</guid>
      <dc:creator>KDS_1113</dc:creator>
      <dc:date>2017-02-20T22:58:50Z</dc:date>
    </item>
    <item>
      <title>Re: Converting Character to Numeric and Removing Quotes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-Character-to-Numeric-and-Removing-Quotes/m-p/334585#M75577</link>
      <description>&lt;P&gt;You will find explanation to my code in the attached documnent.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In any case you need more informatin on a SAS term like statement, function etc. you&lt;/P&gt;
&lt;P&gt;can search by google for &amp;nbsp;&lt;STRONG&gt;sas support &amp;lt;term&amp;gt;&amp;nbsp;&lt;/STRONG&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For &lt;STRONG&gt;INFILE&lt;/STRONG&gt; statement you may look at:&lt;BR /&gt;&lt;A href="https://support.sas.com/documentation/cdl/en/lestmtsref/63323/HTML/default/viewer.htm#n1rill4udj0tfun1fvce3j401plo.htm" target="_self"&gt;https://support.sas.com/documentation/cdl/en/lestmtsref/63323/HTML/default/viewer.htm#n1rill4udj0tfun1fvce3j401plo.htm&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 21 Feb 2017 08:12:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-Character-to-Numeric-and-Removing-Quotes/m-p/334585#M75577</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2017-02-21T08:12:12Z</dc:date>
    </item>
    <item>
      <title>Re: Converting Character to Numeric and Removing Quotes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-Character-to-Numeric-and-Removing-Quotes/m-p/334655#M75593</link>
      <description>&lt;P&gt;Shmuel,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you SO much! Your explanation was better than anything i've come across online (or in class!). &amp;nbsp;I feel more comfortable trying your approach now.&lt;/P&gt;</description>
      <pubDate>Tue, 21 Feb 2017 14:05:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-Character-to-Numeric-and-Removing-Quotes/m-p/334655#M75593</guid>
      <dc:creator>KDS_1113</dc:creator>
      <dc:date>2017-02-21T14:05:57Z</dc:date>
    </item>
  </channel>
</rss>

