<?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 array of numeric columns to character columns in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Converting-array-of-numeric-columns-to-character-columns/m-p/742186#M232087</link>
    <description>&lt;P&gt;Do&amp;nbsp;&lt;STRONG&gt;NOT&lt;/STRONG&gt; use PROC IMPORT for CSV files. Write the data step that reads the file yourself, don't let the computer handle that for you (Maxim 31).&lt;/P&gt;
&lt;P&gt;At least take the data step that IMPORT wrote for you from the log and adapt it to your needs.&lt;/P&gt;</description>
    <pubDate>Tue, 18 May 2021 14:51:20 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2021-05-18T14:51:20Z</dc:date>
    <item>
      <title>Converting array of numeric columns to character columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-array-of-numeric-columns-to-character-columns/m-p/742181#M232084</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm working with some tricky raw data and need to convert all numeric columns in an imported .csv file to character type.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P data-unlink="true"&gt;I've followed &lt;A href="https://chemicalstatistician.wordpress.com/2018/04/27/convert-multiple-variables-between-character-and-numeric-formats-in-sas/" target="_self"&gt;this tutorial&lt;/A&gt;&amp;nbsp;and got close to where I need to be, but I think something in the final data step is reverting the type of the columns back to numeric and I get the below note in the log:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;NOTE: Character values have been converted to numeric values at the places given by: (Line):(Column).
      35:3   &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P data-unlink="true"&gt;Can anyone see anything in my code that might explain this or is able to explain the log note's meaning a bit further for me so I can find the issue myself.&lt;/P&gt;
&lt;P data-unlink="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P data-unlink="true"&gt;My code looks like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Import April 2019 form */
PROC IMPORT OUT= raw_new_neo_april19
            DATAFILE= ***confidental*** 
            DBMS=CSV REPLACE;
     GETNAMES=YES;
     DATAROW=2;   
	 GUESSINGROWS=2000;
RUN;

/* Create dataset with all column names and type */ 
proc contents
     data = raw_new_neo_april19
          noprint
     out = vars1 (keep = name type);
run;

/* Use SQL to create macros with column names */
proc sql;
     * create macro list of all numeric variables names;
     select name
     into :numerics
          separated by ' '
     from vars1
     where type = 1;

     * create macro list of the numeric variables' names with the suffix "C";
     select trim(name) || 'C'
     into :characters
          separated by ' '
     from vars1
     where type = 1;

     * create macro list of the conversion from the original name to the new name with the suffix "C";
     select cats(name, ' = ' , name, 'C')
     into :conversions
          separated by ' '
     from vars1
     where type = 1;
quit;

/* Check column names all look okay */
%PUT &amp;amp;numerics;
%PUT &amp;amp;characters;
%PUT &amp;amp;conversions;

/* use the macro list "&amp;amp;conversions" to add the suffix "C" to the numeric variables' names */
proc datasets 
     library = work 
          nolist;
     modify raw_new_neo_april19;
     rename &amp;amp;conversions;
quit;
&lt;BR /&gt;/* Use arrays and put to convert all numeric columns to character columns */
data raw_new_neo_april19_2;
     set raw_new_neo_april19;


     array nums[*] &amp;amp;numerics;
     array chars[*] &amp;amp;characters;

     do i = 1 to dim(nums);          
		nums[i] = put(chars[i],10.);
     end;

     drop i &amp;amp;characters;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 18 May 2021 14:46:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-array-of-numeric-columns-to-character-columns/m-p/742181#M232084</guid>
      <dc:creator>RoddyJ</dc:creator>
      <dc:date>2021-05-18T14:46:30Z</dc:date>
    </item>
    <item>
      <title>Re: Converting array of numeric columns to character columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-array-of-numeric-columns-to-character-columns/m-p/742183#M232085</link>
      <description>&lt;P&gt;Then don't use PROC IMPORT.&amp;nbsp; Converting them back to character after reading them into numbers is likely to lose information.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Just write your own data step to read the text file directly.&lt;/P&gt;
&lt;P&gt;If you really have no idea what is in the file look at the code that PROC IMPORT generated and use it as a model (although PROC IMPORT writes messy code).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you just want to read everything as character it is really easy as all you need to do is fill out the LENGTH statement to define how long each variable needs to be.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  infile "myfile" dsd firstobs=2 truncover ;
  length first_var $10 next_var $20 .... last_var $30 ;
  input first_var -- last_var;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 18 May 2021 14:56:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-array-of-numeric-columns-to-character-columns/m-p/742183#M232085</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-05-18T14:56:55Z</dc:date>
    </item>
    <item>
      <title>Re: Converting array of numeric columns to character columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-array-of-numeric-columns-to-character-columns/m-p/742186#M232087</link>
      <description>&lt;P&gt;Do&amp;nbsp;&lt;STRONG&gt;NOT&lt;/STRONG&gt; use PROC IMPORT for CSV files. Write the data step that reads the file yourself, don't let the computer handle that for you (Maxim 31).&lt;/P&gt;
&lt;P&gt;At least take the data step that IMPORT wrote for you from the log and adapt it to your needs.&lt;/P&gt;</description>
      <pubDate>Tue, 18 May 2021 14:51:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-array-of-numeric-columns-to-character-columns/m-p/742186#M232087</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-05-18T14:51:20Z</dc:date>
    </item>
    <item>
      <title>Re: Converting array of numeric columns to character columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-array-of-numeric-columns-to-character-columns/m-p/742191#M232090</link>
      <description>&lt;P&gt;Thanks for the speedy reply!&lt;/P&gt;
&lt;P&gt;The csv has 195 columns, is there a neat and tidy way to import all columns as character variables in a data step without typing out all their names and taking several lines of code?&lt;/P&gt;</description>
      <pubDate>Tue, 18 May 2021 15:03:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-array-of-numeric-columns-to-character-columns/m-p/742191#M232090</guid>
      <dc:creator>RoddyJ</dc:creator>
      <dc:date>2021-05-18T15:03:34Z</dc:date>
    </item>
    <item>
      <title>Re: Converting array of numeric columns to character columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-array-of-numeric-columns-to-character-columns/m-p/742193#M232092</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/289103"&gt;@RoddyJ&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi all,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm working with some tricky raw data and need to convert all numeric columns in an imported .csv file to character type.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P data-unlink="true"&gt;I've followed &lt;A href="https://chemicalstatistician.wordpress.com/2018/04/27/convert-multiple-variables-between-character-and-numeric-formats-in-sas/" target="_self"&gt;this tutorial&lt;/A&gt;&amp;nbsp;and got close to where I need to be, but I think something in the final data step is reverting the type of the columns back to numeric and I get the below note in the log:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;NOTE: Character values have been converted to numeric values at the places given by: (Line):(Column).
      35:3   &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P data-unlink="true"&gt;Can anyone see anything in my code that might explain this or is able to explain the log note's meaning a bit further for me so I can find the issue myself.&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;If you show us the entire log for the data step or procedure that NOTE so we can see which is line 35 we might have a chance.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Caution is in order with creating variable names by code when adding characters to the name. If the variable name already is 32 characters your "suffix" will either get truncated, ignored or cause errors depending on where it is used.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Since you are only changing "some" variables there is also a chance you have a collision with another existing variable name.&amp;nbsp; If you have a character variable named "AgeC" already in your data and a numeric variable "Age" then this approach has an opportunity to get confused or generate an error.&lt;/P&gt;</description>
      <pubDate>Tue, 18 May 2021 15:08:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-array-of-numeric-columns-to-character-columns/m-p/742193#M232092</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-05-18T15:08:20Z</dc:date>
    </item>
    <item>
      <title>Re: Converting array of numeric columns to character columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-array-of-numeric-columns-to-character-columns/m-p/742194#M232093</link>
      <description>&lt;P&gt;Do these columns all share the same length?&lt;/P&gt;</description>
      <pubDate>Tue, 18 May 2021 15:10:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-array-of-numeric-columns-to-character-columns/m-p/742194#M232093</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-05-18T15:10:07Z</dc:date>
    </item>
    <item>
      <title>Re: Converting array of numeric columns to character columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-array-of-numeric-columns-to-character-columns/m-p/742196#M232094</link>
      <description>Thanks for the response, this is the full log&lt;BR /&gt;&lt;BR /&gt;78        &lt;BR /&gt;679        data raw_new_neo_april19_2;&lt;BR /&gt;680             set raw_new_neo_april19;&lt;BR /&gt;681        &lt;BR /&gt;682        &lt;BR /&gt;683             array nums[*] &amp;amp;numerics;&lt;BR /&gt;684             array chars[*] &amp;amp;characters;&lt;BR /&gt;685        &lt;BR /&gt;686             do i = 1 to dim(nums);&lt;BR /&gt;687        		nums[i] = put(chars[i],10.);&lt;BR /&gt;688             end;&lt;BR /&gt;689        &lt;BR /&gt;690             drop i &amp;amp;characters;&lt;BR /&gt;691        run;&lt;BR /&gt;&lt;BR /&gt;NOTE: Character values have been converted to numeric values at the places given by: (Line):(Column).&lt;BR /&gt;      687:3   &lt;BR /&gt;NOTE: There were 35 observations read from the data set WORK.RAW_NEW_NEO_APRIL19.&lt;BR /&gt;NOTE: The data set WORK.RAW_NEW_NEO_APRIL19_2 has 35 observations and 195 variables.&lt;BR /&gt;NOTE: DATA statement used (Total process time):&lt;BR /&gt;      real time           0.01 seconds&lt;BR /&gt;      cpu time            0.00 seconds</description>
      <pubDate>Tue, 18 May 2021 15:10:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-array-of-numeric-columns-to-character-columns/m-p/742196#M232094</guid>
      <dc:creator>RoddyJ</dc:creator>
      <dc:date>2021-05-18T15:10:48Z</dc:date>
    </item>
    <item>
      <title>Re: Converting array of numeric columns to character columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-array-of-numeric-columns-to-character-columns/m-p/742198#M232095</link>
      <description>Unfortunately not, but willing to have leading blanks or suboptimal length allocation to sort later</description>
      <pubDate>Tue, 18 May 2021 15:12:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-array-of-numeric-columns-to-character-columns/m-p/742198#M232095</guid>
      <dc:creator>RoddyJ</dc:creator>
      <dc:date>2021-05-18T15:12:14Z</dc:date>
    </item>
    <item>
      <title>Re: Converting array of numeric columns to character columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-array-of-numeric-columns-to-character-columns/m-p/742200#M232097</link>
      <description>&lt;P&gt;Here&lt;/P&gt;
&lt;PRE&gt;684 array chars[*] &amp;amp;characters;&lt;/PRE&gt;
&lt;P&gt;you did not specify that the elements of the array should be character.&lt;/P&gt;</description>
      <pubDate>Tue, 18 May 2021 15:12:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-array-of-numeric-columns-to-character-columns/m-p/742200#M232097</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-05-18T15:12:31Z</dc:date>
    </item>
    <item>
      <title>Re: Converting array of numeric columns to character columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-array-of-numeric-columns-to-character-columns/m-p/742203#M232099</link>
      <description>That's done it, thank you so much!</description>
      <pubDate>Tue, 18 May 2021 15:15:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-array-of-numeric-columns-to-character-columns/m-p/742203#M232099</guid>
      <dc:creator>RoddyJ</dc:creator>
      <dc:date>2021-05-18T15:15:16Z</dc:date>
    </item>
    <item>
      <title>Re: Converting array of numeric columns to character columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-array-of-numeric-columns-to-character-columns/m-p/742205#M232100</link>
      <description>&lt;P&gt;So you WILL have to do a lot of manual work anyway.&lt;/P&gt;
&lt;P&gt;As I said, copy/paste the header line into the INPUT statement. Then, split it up (every variable on its own line), and add the informat to set the length:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;input
  var1 :$3.
  var2 :$35.
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and so on.&lt;/P&gt;</description>
      <pubDate>Tue, 18 May 2021 15:15:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-array-of-numeric-columns-to-character-columns/m-p/742205#M232100</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-05-18T15:15:45Z</dc:date>
    </item>
    <item>
      <title>Re: Converting array of numeric columns to character columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-array-of-numeric-columns-to-character-columns/m-p/742211#M232104</link>
      <description>&lt;P&gt;If the column headers are valid SAS variable names you can easily read the first line and store it into a macro variable to use to generate the step to read the data.&lt;/P&gt;
&lt;P&gt;For example this code will read everything in as length 100.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename csv "myfilename goes here";
data _null_;
  infile csv obs=1;
  input;
  call symputx('varnames',translate(_infile_,' ',','));
run;
data want (compress=yes);
  infile csv dsd firstobs=2 truncover ;
  input (&amp;amp;varnames) (:$100.);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 18 May 2021 15:31:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-array-of-numeric-columns-to-character-columns/m-p/742211#M232104</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-05-18T15:31:47Z</dc:date>
    </item>
    <item>
      <title>Re: Converting array of numeric columns to character columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-array-of-numeric-columns-to-character-columns/m-p/744457#M233232</link>
      <description>Thanks Tom, this is a very good solution!</description>
      <pubDate>Fri, 28 May 2021 16:27:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-array-of-numeric-columns-to-character-columns/m-p/744457#M233232</guid>
      <dc:creator>RoddyJ</dc:creator>
      <dc:date>2021-05-28T16:27:04Z</dc:date>
    </item>
  </channel>
</rss>

