<?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: Importing dataset with dates as column headers in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Importing-dataset-with-dates-as-column-headers/m-p/334213#M75468</link>
    <description>&lt;P&gt;Art297, &amp;nbsp;thank you!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm a student and just learning SAS, so can you help me understand &amp;nbsp;the proc sql statement you provided? It appears you're creating a macro (I'm just learnning macros and find them somewhat confusing). &amp;nbsp;Can you explain what the sections with arrows are doing?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sql noprint;&lt;BR /&gt;select catx('=y',name, &amp;nbsp;&amp;lt;&amp;lt;&amp;lt;---------&lt;BR /&gt;year(input(translate(substr(name,2),'/','_'),anydtdte.)))&lt;BR /&gt;into :vnames separated by ' '&lt;BR /&gt;from dictionary.columns &amp;lt;&amp;lt;&amp;lt;&amp;lt;--------&lt;BR /&gt;where libname='WORK' and&lt;BR /&gt;memname='NEWFILE' and &amp;lt;&amp;lt;&amp;lt;&amp;lt;----- &amp;nbsp;what is memname?&lt;BR /&gt;first(name) eq '_' &amp;nbsp;&amp;lt;&amp;lt;&amp;lt;---- is this calling on name created in line 2?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 19 Feb 2017 19:22:59 GMT</pubDate>
    <dc:creator>KDS_1113</dc:creator>
    <dc:date>2017-02-19T19:22:59Z</dc:date>
    <item>
      <title>Importing dataset with dates as column headers</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Importing-dataset-with-dates-as-column-headers/m-p/334194#M75459</link>
      <description>&lt;P&gt;Morning -&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a .csv data set (not created by me) that contains city, state and then population by year (i.e., 1/1/2015, 1/1/2016, etc...). &amp;nbsp;When I import the data into sas using the infile statement, it coverts the headers to _1_1_2015, _1_1_2016. &amp;nbsp;I would prefer my column headers to be something like Y2015, Y2016, etc. &amp;nbsp;As a way to get around this, I condsidered NOT importing the column headers and just later renaming them, but I'm wondering there's a better way to go about this.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here's an example of my code:&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc import&lt;BR /&gt;datafile="C:\example.csv"&lt;BR /&gt;dbms=csv&lt;BR /&gt;out=work.newfile&lt;BR /&gt;replace;&lt;BR /&gt;getnames=no;&lt;BR /&gt;datarows=2;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Next, I thought I could create a new table using a proc sql statement and renaming each of the date columns. &amp;nbsp;I imagine there has to be a better way to go about this. &amp;nbsp;Any advice?&lt;/P&gt;</description>
      <pubDate>Sun, 19 Feb 2017 17:20:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Importing-dataset-with-dates-as-column-headers/m-p/334194#M75459</guid>
      <dc:creator>KDS_1113</dc:creator>
      <dc:date>2017-02-19T17:20:43Z</dc:date>
    </item>
    <item>
      <title>Re: Importing dataset with dates as column headers</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Importing-dataset-with-dates-as-column-headers/m-p/334197#M75461</link>
      <description>&lt;P&gt;Transpose to a long format and then use scan to extract the year.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you really want a wide format, transpose back to a long format using proc transpose and ID variable as the Year variable.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 19 Feb 2017 17:42:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Importing-dataset-with-dates-as-column-headers/m-p/334197#M75461</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-02-19T17:42:28Z</dc:date>
    </item>
    <item>
      <title>Re: Importing dataset with dates as column headers</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Importing-dataset-with-dates-as-column-headers/m-p/334200#M75462</link>
      <description>&lt;P&gt;How many years' population do you have in the csv file ?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can you &lt;U&gt;&lt;STRONG&gt;post a sample of your csv file&lt;/STRONG&gt;&lt;/U&gt; - just few rows to be used a test file ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You wrote:&lt;STRONG&gt;&amp;nbsp;When I import the data into sas using the infile statement, it coverts the headers to _1_1_2015&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;I believe the header is created by PROC IMPORT and not throght data step with INFILE statement.&lt;/P&gt;
&lt;P&gt;Using infile you have better control on variable names (headers) and data format.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can adapt next code to your needs:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* filename pop '...path and name.csv'; */
data want;
    infile datalines /* pop */ dlm=',' truncover firstobs=2;
    input city $ state $ pop2015 - pop2017;
    format pop: comma15.;
datalines;
CITY, state,01/01/2015,01/01/2016,01/01/2017
city name a, st1, 200000, 200200, 202,500
mini town, st2, 5000, 4899, 5005
capital, vw, 10200500, 10220000, 101900800
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 19 Feb 2017 18:24:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Importing-dataset-with-dates-as-column-headers/m-p/334200#M75462</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2017-02-19T18:24:44Z</dc:date>
    </item>
    <item>
      <title>Re: Importing dataset with dates as column headers</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Importing-dataset-with-dates-as-column-headers/m-p/334202#M75463</link>
      <description>&lt;P&gt;proc import writes the data step it creates to the log. Copy that from there and replace the column names.&lt;/P&gt;
&lt;P&gt;If you need that process automated, you can read the original column names from the first line and then create a data step dynamically with call execute.&lt;/P&gt;
&lt;P&gt;Please post some example lines from the csv.&lt;/P&gt;</description>
      <pubDate>Sun, 19 Feb 2017 18:22:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Importing-dataset-with-dates-as-column-headers/m-p/334202#M75463</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-02-19T18:22:25Z</dc:date>
    </item>
    <item>
      <title>Re: Importing dataset with dates as column headers</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Importing-dataset-with-dates-as-column-headers/m-p/334204#M75464</link>
      <description>&lt;P&gt;Thanks all -&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Attached is a sample of my data.&lt;/P&gt;</description>
      <pubDate>Sun, 19 Feb 2017 18:27:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Importing-dataset-with-dates-as-column-headers/m-p/334204#M75464</guid>
      <dc:creator>KDS_1113</dc:creator>
      <dc:date>2017-02-19T18:27:51Z</dc:date>
    </item>
    <item>
      <title>Re: Importing dataset with dates as column headers</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Importing-dataset-with-dates-as-column-headers/m-p/334205#M75465</link>
      <description>&lt;P&gt;THAT is not a csv.&lt;/P&gt;
&lt;P&gt;Please post the top three lines&amp;nbsp;&lt;U&gt;of the original csv file&lt;/U&gt;&lt;U&gt;&lt;/U&gt;&lt;U&gt;&lt;/U&gt; as&amp;nbsp;&lt;U&gt;text&lt;/U&gt;.&lt;/P&gt;
&lt;P&gt;An Excel file (aside from the fact that it could contain malware) is utterly useless for writing a data step to import a csv. That should be obvious.&lt;/P&gt;</description>
      <pubDate>Sun, 19 Feb 2017 18:40:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Importing-dataset-with-dates-as-column-headers/m-p/334205#M75465</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-02-19T18:40:55Z</dc:date>
    </item>
    <item>
      <title>Re: Importing dataset with dates as column headers</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Importing-dataset-with-dates-as-column-headers/m-p/334206#M75466</link>
      <description>&lt;P&gt;I like your idea of using SQL to rename the variables. Since you said it was a comma delimited file, I save your example file as such and accomplished the task as follows:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;proc import
    datafile="C:\art\samplefile-pop.csv"
    dbms=csv
    out=work.newfile
    replace;
  getnames=yes;
  datarow=2;
run;

proc sql noprint;
  select catx('=y',name,
    year(input(translate(substr(name,2),'/','_'),anydtdte.)))
     into :vnames separated by ' '
       from dictionary.columns
         where libname='WORK' and
               memname='NEWFILE' and
               first(name) eq '_'
  ;
quit;

data newfile;
  set newfile (rename=(&amp;amp;vnames.));
run;
&lt;/PRE&gt;
&lt;P&gt;HTH,&lt;/P&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 19 Feb 2017 18:46:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Importing-dataset-with-dates-as-column-headers/m-p/334206#M75466</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-02-19T18:46:44Z</dc:date>
    </item>
    <item>
      <title>Re: Importing dataset with dates as column headers</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Importing-dataset-with-dates-as-column-headers/m-p/334212#M75467</link>
      <description>&lt;P&gt;I saved the sample file as an excel file in error. &amp;nbsp;The original IS a csv. &amp;nbsp;Attached&amp;nbsp;the first 3 lines of the original csv saved as text.&lt;/P&gt;</description>
      <pubDate>Sun, 19 Feb 2017 19:10:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Importing-dataset-with-dates-as-column-headers/m-p/334212#M75467</guid>
      <dc:creator>KDS_1113</dc:creator>
      <dc:date>2017-02-19T19:10:58Z</dc:date>
    </item>
    <item>
      <title>Re: Importing dataset with dates as column headers</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Importing-dataset-with-dates-as-column-headers/m-p/334213#M75468</link>
      <description>&lt;P&gt;Art297, &amp;nbsp;thank you!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm a student and just learning SAS, so can you help me understand &amp;nbsp;the proc sql statement you provided? It appears you're creating a macro (I'm just learnning macros and find them somewhat confusing). &amp;nbsp;Can you explain what the sections with arrows are doing?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sql noprint;&lt;BR /&gt;select catx('=y',name, &amp;nbsp;&amp;lt;&amp;lt;&amp;lt;---------&lt;BR /&gt;year(input(translate(substr(name,2),'/','_'),anydtdte.)))&lt;BR /&gt;into :vnames separated by ' '&lt;BR /&gt;from dictionary.columns &amp;lt;&amp;lt;&amp;lt;&amp;lt;--------&lt;BR /&gt;where libname='WORK' and&lt;BR /&gt;memname='NEWFILE' and &amp;lt;&amp;lt;&amp;lt;&amp;lt;----- &amp;nbsp;what is memname?&lt;BR /&gt;first(name) eq '_' &amp;nbsp;&amp;lt;&amp;lt;&amp;lt;---- is this calling on name created in line 2?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 19 Feb 2017 19:22:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Importing-dataset-with-dates-as-column-headers/m-p/334213#M75468</guid>
      <dc:creator>KDS_1113</dc:creator>
      <dc:date>2017-02-19T19:22:59Z</dc:date>
    </item>
    <item>
      <title>Re: Importing dataset with dates as column headers</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Importing-dataset-with-dates-as-column-headers/m-p/334215#M75469</link>
      <description>&lt;P&gt;I'll answer your questions in reverse.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;first(name) eq '_' &amp;nbsp;&amp;lt;&amp;lt;&amp;lt;---- is this calling on name created in line 2? No.&amp;nbsp;I describe NAME, below.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;from dictionary.columns &amp;lt;&amp;lt;&amp;lt;&amp;lt;--------&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;where libname='WORK' and&lt;BR /&gt;memname='NEWFILE' and &amp;lt;&amp;lt;&amp;lt;&amp;lt;----- &amp;nbsp;what is memname?&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;dictionary.columns is a table that contains all of the column headers (i.e., variable names) for every sas dataset that is identified with a libname. One of its fields is called NAME, which holds&amp;nbsp;the variable names.&lt;/P&gt;
&lt;P&gt;MEMNAME&amp;nbsp;is the field that contains the dataset names and libname is the field that contains the library names. Their values, in dictionary.columns, are ALL UPPERCASE. So, in this case, the code is getting the NAMEs of all of the variables in work.newfile&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So, going back to&amp;nbsp;&lt;SPAN&gt;first(name) eq '_', FIRST is a function that captures the first character of a variable's value. So, in this case, the where statement is selecting only those variables from work.newname whose variable name starts with a '_' character (namely, your date columns).&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;select catx('=y',name, &amp;nbsp;&amp;lt;&amp;lt;&amp;lt;---------&lt;BR /&gt;year(input(translate(substr(name,2),'/','_'),anydt&lt;WBR /&gt;dte.)))&lt;BR /&gt;into :vnames separated by ' '&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Catx is a function that concatenates strings together, separating them by the first string listed. The into :vnames separated by ' ' creates a macro variable. So, the full statement is taking each name that begins with an underscore (i.e., _2000_01_01, _01_01_2001, _2002_01_01, _2003_01_01, _2004_01_01,&amp;nbsp;_2005_01_01, _2006_01_01&amp;nbsp;and_2007_01_01), and converting them into one long string, namely _2000_01_01=y2000 _2001_01_01=y2001 _2002_01_01=y2002 _2003_01_01=y2003 _2004_01_01=y2004 _2005_01_01=y2005 _2006_01_01=y2006 _2007_01_01=y2007&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;The translate function is used to convert the underscores into forward slashes so that the anydtdte. informat will read the values as dates. The statement starts with the year function which extracts the years from the values.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;The macro variable that is created simply contains the above string, which just happens to be in the exact same format that the recode option calls for.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;HTH,&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Art, CEO, AnalystFinder.com&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 19 Feb 2017 19:54:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Importing-dataset-with-dates-as-column-headers/m-p/334215#M75469</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-02-19T19:54:27Z</dc:date>
    </item>
    <item>
      <title>Re: Importing dataset with dates as column headers</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Importing-dataset-with-dates-as-column-headers/m-p/334216#M75470</link>
      <description>&lt;P&gt;Attached is the code to be done in one step. Just adapt macro variables on top to desired years.&lt;/P&gt;
&lt;P&gt;You can also change the prefix from POP to Y if you like.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename pop '/folders/myshortcuts/My_Folders/flat/samplefile-pop.csv'; 
%let from_year = 2000;
%let upto_year = 2007;
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)); 
 put city= state=  pos=;
    pos = pos + lengthn(state) +2; 
    popx = compress(substr(a_line,pos),','); put pos= popx=;
    
    array pop pop&amp;amp;from_year - pop&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 pop&amp;amp;from_year - pop&amp;amp;upto_year;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 19 Feb 2017 20:09:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Importing-dataset-with-dates-as-column-headers/m-p/334216#M75470</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2017-02-19T20:09:40Z</dc:date>
    </item>
    <item>
      <title>Re: Importing dataset with dates as column headers</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Importing-dataset-with-dates-as-column-headers/m-p/334222#M75471</link>
      <description>Thank you for taking the time to answer my questions in detail.</description>
      <pubDate>Sun, 19 Feb 2017 21:11:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Importing-dataset-with-dates-as-column-headers/m-p/334222#M75471</guid>
      <dc:creator>KDS_1113</dc:creator>
      <dc:date>2017-02-19T21:11:17Z</dc:date>
    </item>
  </channel>
</rss>

