<?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: Import CSV with spaces in variable names using DATA step in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Import-CSV-with-spaces-in-variable-names-using-DATA-step/m-p/491451#M128886</link>
    <description>&lt;P&gt;If you're using a data step you would specify the variable list in your INPUT statement.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you're trying&amp;nbsp;to do this dynamically&amp;nbsp;you could either consider NLITERAL() function or somehow add an underscore before hand.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How is &amp;amp;varlist being created?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/80715"&gt;@viola&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I am having trouble importing a CSV file using the DATA step where some of the column names have spaces. I have specified that a comma is the delimiter, but SAS is still breaking up the variable names into multiple columns. I am aware that using PROC IMPORT would solve this problem, but for various reasons, is not an option in my case. What am I missing?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data &amp;amp;&amp;amp;sasname&amp;amp;i;
	infile "&amp;amp;&amp;amp;path&amp;amp;i" delimiter="," dsd firstobs=2 truncover;
	input &amp;amp;varlist; 
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;My CSV looks like the following:&lt;/P&gt;
&lt;P&gt;Visit Repeat Key,Form Repeat Key,Item Group Repeat Key,Scheduled Date,Completed Date&lt;BR /&gt;1,1,,2018-08-01T07:00:00.000Z,2018-08-02T18:11:17.000Z&lt;BR /&gt;1,1,,2018-08-02T07:00:00.000Z,2018-08-02T23:14:02.000Z&lt;BR /&gt;1,1,,2018-08-01T07:00:00.000Z,2018-08-02T17:11:16.000Z&lt;BR /&gt;1,1,,2018-08-01T07:00:00.000Z,2018-08-02T17:00:21.000Z&lt;BR /&gt;1,1,,2018-08-21T07:00:00.000Z,&lt;BR /&gt;1,1,,2018-08-01T07:00:00.000Z,2018-08-02T19:59:46.000Z&lt;BR /&gt;1,1,,2018-08-02T07:00:00.000Z,2018-08-02T16:58:20.000Z&lt;BR /&gt;1,1,,2018-08-01T07:00:00.000Z,2018-08-02T17:12:31.000Z&lt;BR /&gt;1,1,,2018-08-01T07:00:00.000Z,2018-08-02T17:53:10.000Z&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 30 Aug 2018 22:05:08 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2018-08-30T22:05:08Z</dc:date>
    <item>
      <title>Import CSV with spaces in variable names using DATA step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-CSV-with-spaces-in-variable-names-using-DATA-step/m-p/491447#M128882</link>
      <description>&lt;P&gt;I am having trouble importing a CSV file using the DATA step where some of the column names have spaces. I have specified that a comma is the delimiter, but SAS is still breaking up the variable names into multiple columns. I am aware that using PROC IMPORT would solve this problem, but for various reasons, is not an option in my case. What am I missing?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data &amp;amp;&amp;amp;sasname&amp;amp;i;
	infile "&amp;amp;&amp;amp;path&amp;amp;i" delimiter="," dsd firstobs=2 truncover;
	input &amp;amp;varlist; 
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;My CSV looks like the following:&lt;/P&gt;&lt;P&gt;Visit Repeat Key,Form Repeat Key,Item Group Repeat Key,Scheduled Date,Completed Date&lt;BR /&gt;1,1,,2018-08-01T07:00:00.000Z,2018-08-02T18:11:17.000Z&lt;BR /&gt;1,1,,2018-08-02T07:00:00.000Z,2018-08-02T23:14:02.000Z&lt;BR /&gt;1,1,,2018-08-01T07:00:00.000Z,2018-08-02T17:11:16.000Z&lt;BR /&gt;1,1,,2018-08-01T07:00:00.000Z,2018-08-02T17:00:21.000Z&lt;BR /&gt;1,1,,2018-08-21T07:00:00.000Z,&lt;BR /&gt;1,1,,2018-08-01T07:00:00.000Z,2018-08-02T19:59:46.000Z&lt;BR /&gt;1,1,,2018-08-02T07:00:00.000Z,2018-08-02T16:58:20.000Z&lt;BR /&gt;1,1,,2018-08-01T07:00:00.000Z,2018-08-02T17:12:31.000Z&lt;BR /&gt;1,1,,2018-08-01T07:00:00.000Z,2018-08-02T17:53:10.000Z&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 30 Aug 2018 21:59:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-CSV-with-spaces-in-variable-names-using-DATA-step/m-p/491447#M128882</guid>
      <dc:creator>viola</dc:creator>
      <dc:date>2018-08-30T21:59:15Z</dc:date>
    </item>
    <item>
      <title>Re: Import CSV with spaces in variable names using DATA step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-CSV-with-spaces-in-variable-names-using-DATA-step/m-p/491451#M128886</link>
      <description>&lt;P&gt;If you're using a data step you would specify the variable list in your INPUT statement.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you're trying&amp;nbsp;to do this dynamically&amp;nbsp;you could either consider NLITERAL() function or somehow add an underscore before hand.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How is &amp;amp;varlist being created?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/80715"&gt;@viola&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I am having trouble importing a CSV file using the DATA step where some of the column names have spaces. I have specified that a comma is the delimiter, but SAS is still breaking up the variable names into multiple columns. I am aware that using PROC IMPORT would solve this problem, but for various reasons, is not an option in my case. What am I missing?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data &amp;amp;&amp;amp;sasname&amp;amp;i;
	infile "&amp;amp;&amp;amp;path&amp;amp;i" delimiter="," dsd firstobs=2 truncover;
	input &amp;amp;varlist; 
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;My CSV looks like the following:&lt;/P&gt;
&lt;P&gt;Visit Repeat Key,Form Repeat Key,Item Group Repeat Key,Scheduled Date,Completed Date&lt;BR /&gt;1,1,,2018-08-01T07:00:00.000Z,2018-08-02T18:11:17.000Z&lt;BR /&gt;1,1,,2018-08-02T07:00:00.000Z,2018-08-02T23:14:02.000Z&lt;BR /&gt;1,1,,2018-08-01T07:00:00.000Z,2018-08-02T17:11:16.000Z&lt;BR /&gt;1,1,,2018-08-01T07:00:00.000Z,2018-08-02T17:00:21.000Z&lt;BR /&gt;1,1,,2018-08-21T07:00:00.000Z,&lt;BR /&gt;1,1,,2018-08-01T07:00:00.000Z,2018-08-02T19:59:46.000Z&lt;BR /&gt;1,1,,2018-08-02T07:00:00.000Z,2018-08-02T16:58:20.000Z&lt;BR /&gt;1,1,,2018-08-01T07:00:00.000Z,2018-08-02T17:12:31.000Z&lt;BR /&gt;1,1,,2018-08-01T07:00:00.000Z,2018-08-02T17:53:10.000Z&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 30 Aug 2018 22:05:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-CSV-with-spaces-in-variable-names-using-DATA-step/m-p/491451#M128886</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-08-30T22:05:08Z</dc:date>
    </item>
    <item>
      <title>Re: Import CSV with spaces in variable names using DATA step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-CSV-with-spaces-in-variable-names-using-DATA-step/m-p/491463#M128889</link>
      <description>The varlist is being created in a previous data _null_ step using the header row. Code does need to be dynamic.&lt;BR /&gt;&lt;BR /&gt;data _null_;&lt;BR /&gt;infile "&amp;amp;&amp;amp;path&amp;amp;i" obs=1 delimiter=",";&lt;BR /&gt;input;&lt;BR /&gt;call symputx('varlist',tranwrd(compress(_infile_,'"'),',',' '));&lt;BR /&gt;run;</description>
      <pubDate>Thu, 30 Aug 2018 22:38:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-CSV-with-spaces-in-variable-names-using-DATA-step/m-p/491463#M128889</guid>
      <dc:creator>viola</dc:creator>
      <dc:date>2018-08-30T22:38:51Z</dc:date>
    </item>
    <item>
      <title>Re: Import CSV with spaces in variable names using DATA step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-CSV-with-spaces-in-variable-names-using-DATA-step/m-p/491466#M128891</link>
      <description>&lt;P&gt;You can either compress out the spaces in the variable names, which is probably a good idea, Or you can use NLITERAL like I mentioned to wrap the variable with quotes and add the N at the end to give you a valid SAS name.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you're compressing them out, you likely want to use COUNTW to count the number of commas which will indicate the number of variables. Then use SCAN() to parse it out and apply the COMPRESS() or NLITERAL function as desired.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/80715"&gt;@viola&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;The varlist is being created in a previous data _null_ step using the header row. Code does need to be dynamic.&lt;BR /&gt;&lt;BR /&gt;data _null_;&lt;BR /&gt;infile "&amp;amp;&amp;amp;path&amp;amp;i" obs=1 delimiter=",";&lt;BR /&gt;input;&lt;BR /&gt;call symputx('varlist',tranwrd(compress(_infile_,'"'),',',' '));&lt;BR /&gt;run;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 30 Aug 2018 22:47:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-CSV-with-spaces-in-variable-names-using-DATA-step/m-p/491466#M128891</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-08-30T22:47:37Z</dc:date>
    </item>
    <item>
      <title>Re: Import CSV with spaces in variable names using DATA step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-CSV-with-spaces-in-variable-names-using-DATA-step/m-p/491492#M128907</link>
      <description>&lt;P&gt;SAS variable names must be 32 characters or less and can use only letter, digits and underscores. They cannot start with a number.&lt;/P&gt;
&lt;P&gt;You could try just turning the spaces into underscores.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  infile "&amp;amp;&amp;amp;path&amp;amp;i" obs=1 ;
  input;
  call symputx('varlist',translate(trim(_infile_),' _',', '));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you set the option VALIDVARNAME=ANY then you can have spaces and other characters, but then you need to use name literals.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
 infile "&amp;amp;&amp;amp;path&amp;amp;i" dsd obs=1 eof=save ;
 length varlist $30000 name $32 ;
 input name @@ ;
 varlist =catx(' ',varlist,nliteral(name));
 retain varlist;
return;
save:
call symputx('varlist',varlist);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 31 Aug 2018 02:24:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-CSV-with-spaces-in-variable-names-using-DATA-step/m-p/491492#M128907</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-08-31T02:24:35Z</dc:date>
    </item>
    <item>
      <title>Re: Import CSV with spaces in variable names using DATA step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-CSV-with-spaces-in-variable-names-using-DATA-step/m-p/491539#M128931</link>
      <description>&lt;P&gt;I strongly recommend &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;'s suggestion #1, as working without named literals is much easier in future code based on theis dataset.&lt;/P&gt;</description>
      <pubDate>Fri, 31 Aug 2018 07:26:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-CSV-with-spaces-in-variable-names-using-DATA-step/m-p/491539#M128931</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-08-31T07:26:31Z</dc:date>
    </item>
    <item>
      <title>Re: Import CSV with spaces in variable names using DATA step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-CSV-with-spaces-in-variable-names-using-DATA-step/m-p/491631#M128978</link>
      <description>&lt;P&gt;Why not ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;options validvarname=any;&lt;/P&gt;
&lt;P&gt;proc import datafile=..............&lt;/P&gt;</description>
      <pubDate>Fri, 31 Aug 2018 13:45:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-CSV-with-spaces-in-variable-names-using-DATA-step/m-p/491631#M128978</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2018-08-31T13:45:07Z</dc:date>
    </item>
    <item>
      <title>Re: Import CSV with spaces in variable names using DATA step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-CSV-with-spaces-in-variable-names-using-DATA-step/m-p/491770#M129056</link>
      <description>&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;, not an option here. It would take me too long to explain why but I need to use a data step (see my original post).</description>
      <pubDate>Fri, 31 Aug 2018 22:26:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-CSV-with-spaces-in-variable-names-using-DATA-step/m-p/491770#M129056</guid>
      <dc:creator>viola</dc:creator>
      <dc:date>2018-08-31T22:26:49Z</dc:date>
    </item>
    <item>
      <title>Re: Import CSV with spaces in variable names using DATA step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-CSV-with-spaces-in-variable-names-using-DATA-step/m-p/491772#M129058</link>
      <description>&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;, in this scenario, this creates the varlist - but then when I go to read the file in, how do I make it so that the header row is read in again with no spaces?</description>
      <pubDate>Fri, 31 Aug 2018 22:28:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-CSV-with-spaces-in-variable-names-using-DATA-step/m-p/491772#M129058</guid>
      <dc:creator>viola</dc:creator>
      <dc:date>2018-08-31T22:28:21Z</dc:date>
    </item>
    <item>
      <title>Re: Import CSV with spaces in variable names using DATA step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-CSV-with-spaces-in-variable-names-using-DATA-step/m-p/491776#M129062</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/80715"&gt;@viola&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;, not an option here. It would take me too long to explain why but I need to use a data step (see my original post).&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I routinely use Proc import to build a skeleton of a data step to read files. When you use proc import for a delimited file then the LOG will contain the data step code generated by SAS to read the file. Best is make sure you use a large value for the GUESSINGROWS option to get reasonable results.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Copy that code from the log into the editor and go to town.&lt;/P&gt;
&lt;P&gt;Search and replace variable names as needed, adjust lengths, add labels.&lt;/P&gt;
&lt;P&gt;If you remove the generated statements such as&lt;/P&gt;
&lt;PRE&gt;303   /**********************************************************************
304   *   PRODUCT:   SAS
305   *   VERSION:   9.4
306   *   CREATOR:   External File Interface
307   *   DATE:      31AUG18
308   *   DESC:      Generated SAS Datastep Code
309   *   TEMPLATE SOURCE:  (None Specified.)
310   ***********************************************************************/

&lt;/PRE&gt;
&lt;P&gt;and the ones referenciging _EFFIERR_ no one else may ever know that proc import was used.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This will generate variable names with _ instead of imbedded spaces or special characters&amp;nbsp;or leading numeric characters if your headers have such. If you have variables that have the same column header for more than 32 characters you may get some VARx.&lt;/P&gt;
&lt;P&gt;If you have multiple column heading with the same text that are shorter such as "TOTAL" you will get different variables such as TOTAL2 or TOTAL127 depending on a number of factors I've never spent a lot of time worrying about. I generally make more descriptive names for the such as "Total_income" Total_hours" or what ever.&lt;/P&gt;</description>
      <pubDate>Fri, 31 Aug 2018 22:54:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-CSV-with-spaces-in-variable-names-using-DATA-step/m-p/491776#M129062</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-08-31T22:54:13Z</dc:date>
    </item>
    <item>
      <title>Re: Import CSV with spaces in variable names using DATA step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-CSV-with-spaces-in-variable-names-using-DATA-step/m-p/491782#M129067</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/80715"&gt;@viola&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;, in this scenario, this creates the varlist - but then when I go to read the file in, how do I make it so that the header row is read in again with no spaces?&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I don't understand the question. The code you posted was using FIRSTOBS=2 to skip the header row.&amp;nbsp; So it doesn't matter whether the header row has spaces or not since it is being skipped.&lt;/P&gt;</description>
      <pubDate>Fri, 31 Aug 2018 23:58:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-CSV-with-spaces-in-variable-names-using-DATA-step/m-p/491782#M129067</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-08-31T23:58:04Z</dc:date>
    </item>
    <item>
      <title>Re: Import CSV with spaces in variable names using DATA step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-CSV-with-spaces-in-variable-names-using-DATA-step/m-p/492736#M129537</link>
      <description>Please excuse my brain...trying to think through this on a Friday afternoon was not happening. You are right, of course.</description>
      <pubDate>Wed, 05 Sep 2018 17:13:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-CSV-with-spaces-in-variable-names-using-DATA-step/m-p/492736#M129537</guid>
      <dc:creator>viola</dc:creator>
      <dc:date>2018-09-05T17:13:02Z</dc:date>
    </item>
  </channel>
</rss>

