<?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: Extract year from a CSV file with different formats in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Extract-year-from-a-CSV-file-with-different-formats/m-p/931988#M41914</link>
    <description>&lt;P&gt;I am not at all sure what you mean.&amp;nbsp; But the DDMMYY informat should read the example you show fine to create actual DATE values.&amp;nbsp; You can then use the YEAR() function to get the YEAR from the DATE.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;P&gt;First let's make an example CSV file we can read from:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename csv temp;
options parmcards=csv;
parmcards;
row,date
1,20-03-2014
2,01-10-2015
3,10/10/2014
4,1/10/2013
5,13/4/2010
6,3/3/2012
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now let's read it in and calculate the YEAR.&amp;nbsp; Let's format the DATE variable with a format that will display the date in an unambiguous style.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  infile csv dsd firstobs=2 truncover;
  input row date :ddmmyy.;
  year=year(date);
  format date yymmdd10.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;PRE&gt;Obs    row          date    year

 1      1     2014-03-20    2014
 2      2     2015-10-01    2015
 3      3     2014-10-10    2014
 4      4     2013-10-01    2013
 5      5     2010-04-13    2010
 6      6     2012-03-03    2012
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 12 Jun 2024 18:32:14 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2024-06-12T18:32:14Z</dc:date>
    <item>
      <title>Extract year from a CSV file with different formats</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Extract-year-from-a-CSV-file-with-different-formats/m-p/931979#M41911</link>
      <description>&lt;P&gt;Hey SAS Community,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am new to SAS and I have a Problem with extracting the year from a csv-file with multiple dateformats.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The field 'date' in my csv file has formats like:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;dd-mm-yyyy like 20-03-2014 or 01-10-2015&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;dd/mm/yyyy like 10/10/2014&lt;/P&gt;&lt;P&gt;d/mm/yyyy like 1/10/2013&lt;/P&gt;&lt;P&gt;dd/m/yyyy like 13/4/2010&lt;/P&gt;&lt;P&gt;d/m/yyyy like 3/3/2012&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tried to solve it in different ways and searched in different forums, unfortunately I am not able to solve the problem.&lt;/P&gt;&lt;P&gt;Any help would be very appreciated.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Best Greetings,&lt;/P&gt;&lt;P&gt;Peter&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Edit: Whats also is weird in my txt-editor i can see the different formats, but on sas's result view it shows only one format.&lt;/P&gt;</description>
      <pubDate>Wed, 12 Jun 2024 18:28:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Extract-year-from-a-CSV-file-with-different-formats/m-p/931979#M41911</guid>
      <dc:creator>PeterMiller13</dc:creator>
      <dc:date>2024-06-12T18:28:12Z</dc:date>
    </item>
    <item>
      <title>Re: Extract year from a CSV file with different formats</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Extract-year-from-a-CSV-file-with-different-formats/m-p/931985#M41912</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/466585"&gt;@PeterMiller13&lt;/a&gt;, have you tried the anydtdte. informat? Works wonders, here's the documentation:&amp;nbsp;&lt;A href="https://documentation.sas.com/doc/en/leforinforref/3.2/n04jh1fkv5c8zan14fhqcby7jsu4.htm" target="_blank"&gt;https://documentation.sas.com/doc/en/leforinforref/3.2/n04jh1fkv5c8zan14fhqcby7jsu4.htm&lt;/A&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 12 Jun 2024 18:21:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Extract-year-from-a-CSV-file-with-different-formats/m-p/931985#M41912</guid>
      <dc:creator>antonbcristina</dc:creator>
      <dc:date>2024-06-12T18:21:57Z</dc:date>
    </item>
    <item>
      <title>Re: Extract year from a CSV file with different formats</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Extract-year-from-a-CSV-file-with-different-formats/m-p/931986#M41913</link>
      <description>Yes i tried, it did not work.&lt;BR /&gt;Thank you for your answer!</description>
      <pubDate>Wed, 12 Jun 2024 18:23:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Extract-year-from-a-CSV-file-with-different-formats/m-p/931986#M41913</guid>
      <dc:creator>PeterMiller13</dc:creator>
      <dc:date>2024-06-12T18:23:05Z</dc:date>
    </item>
    <item>
      <title>Re: Extract year from a CSV file with different formats</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Extract-year-from-a-CSV-file-with-different-formats/m-p/931988#M41914</link>
      <description>&lt;P&gt;I am not at all sure what you mean.&amp;nbsp; But the DDMMYY informat should read the example you show fine to create actual DATE values.&amp;nbsp; You can then use the YEAR() function to get the YEAR from the DATE.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;P&gt;First let's make an example CSV file we can read from:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename csv temp;
options parmcards=csv;
parmcards;
row,date
1,20-03-2014
2,01-10-2015
3,10/10/2014
4,1/10/2013
5,13/4/2010
6,3/3/2012
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now let's read it in and calculate the YEAR.&amp;nbsp; Let's format the DATE variable with a format that will display the date in an unambiguous style.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  infile csv dsd firstobs=2 truncover;
  input row date :ddmmyy.;
  year=year(date);
  format date yymmdd10.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;PRE&gt;Obs    row          date    year

 1      1     2014-03-20    2014
 2      2     2015-10-01    2015
 3      3     2014-10-10    2014
 4      4     2013-10-01    2013
 5      5     2010-04-13    2010
 6      6     2012-03-03    2012
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 12 Jun 2024 18:32:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Extract-year-from-a-CSV-file-with-different-formats/m-p/931988#M41914</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-06-12T18:32:14Z</dc:date>
    </item>
    <item>
      <title>Re: Extract year from a CSV file with different formats</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Extract-year-from-a-CSV-file-with-different-formats/m-p/931989#M41915</link>
      <description>&lt;P&gt;If I understand correctly you want to remove the leading zero from day and month for the formatted value?&lt;/P&gt;</description>
      <pubDate>Wed, 12 Jun 2024 18:38:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Extract-year-from-a-CSV-file-with-different-formats/m-p/931989#M41915</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2024-06-12T18:38:26Z</dc:date>
    </item>
    <item>
      <title>Re: Extract year from a CSV file with different formats</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Extract-year-from-a-CSV-file-with-different-formats/m-p/931991#M41916</link>
      <description>&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;Edit: Whats also is weird in my txt-editor i can see the different formats, but on sas's result view it shows only one format.&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;Not sure what that means.&amp;nbsp; &lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;If you are looking at a printout from a SAS dataset in the RESULTS viewer (of whatever interface you are using to run SAS) then it should use the same format to display the value of a single variable.&amp;nbsp; That is what makes a dataset different from a text file (like a CSV file) or a spreadsheet (like an Excel file).&amp;nbsp; The only way to have each date value look different is to not make dates out of them, but instead just read them in as a character variable.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 12 Jun 2024 18:42:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Extract-year-from-a-CSV-file-with-different-formats/m-p/931991#M41916</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-06-12T18:42:53Z</dc:date>
    </item>
  </channel>
</rss>

