<?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: convert character YYYY-mm-dd HH:MM:SS.000000 to date (and overwrite old variables) in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/convert-character-YYYY-mm-dd-HH-MM-SS-000000-to-date-and/m-p/250775#M47311</link>
    <description>&lt;P&gt;In order to replace a character variable with a numeric, do this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=oldvar);
set have (rename=(var=oldvar));
format var someformat.;
var = somefunctionof(oldvar);
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You cannot change the type of a variable in place. You need to create a new variable and use the rename option so that it gets the name of the orginal variable.&lt;/P&gt;</description>
    <pubDate>Thu, 18 Feb 2016 07:36:04 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2016-02-18T07:36:04Z</dc:date>
    <item>
      <title>convert character YYYY-mm-dd HH:MM:SS.000000 to date (and overwrite old variables)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/convert-character-YYYY-mm-dd-HH-MM-SS-000000-to-date-and/m-p/250771#M47309</link>
      <description>&lt;P&gt;Hi - &amp;nbsp;I have imported data into SAS using proc sql to execute a stored procedure.&lt;/P&gt;&lt;P&gt;Unfortunately the date and datetime variables have been imported as character and I would like to convert them appropriately.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Based on this &lt;A href="https://communities.sas.com/t5/SAS-Data-Management/How-to-convert-character-format-yyyymmdd-to-a-date-format/m-p/188996/highlight/true#M3896" target="_self"&gt;post&lt;/A&gt;, I have been able to convert to a new variable (&lt;EM&gt;data_with_new_vars&lt;/EM&gt; below works as expected), however I would like to know if it is possible to overwrite the original variables with the correct format (&lt;EM&gt;data_with_old_vars&lt;/EM&gt; does not work as expected). &amp;nbsp;This is because I want to use the same variable names.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data data_with_char;&lt;BR /&gt; my_date = '2014-09-01 21:19:45.0000000';&lt;BR /&gt; my_datetime = '2014-09-01 21:19:45.0000000';&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;data data_with_new_vars;&lt;BR /&gt;set data_with_char;&lt;BR /&gt; my_date_new = input(my_date, YYMMDD10.);&lt;BR /&gt; format my_date_new date10.;&lt;BR /&gt; my_datetime_new = input(my_datetime, ANYDTDTM19.);&lt;BR /&gt; format my_datetime_new DATETIME21.;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;data data_with_old_vars;&lt;BR /&gt;set data_with_char;&lt;BR /&gt; my_date = input(my_date, YYMMDD10.);&lt;BR /&gt; format my_date date10.;&lt;BR /&gt; my_datetime = input(my_datetime, ANYDTDTM19.);&lt;BR /&gt; format my_datetime DATETIME21.;&lt;BR /&gt;run;&lt;/PRE&gt;</description>
      <pubDate>Thu, 18 Feb 2016 07:12:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/convert-character-YYYY-mm-dd-HH-MM-SS-000000-to-date-and/m-p/250771#M47309</guid>
      <dc:creator>mduarte</dc:creator>
      <dc:date>2016-02-18T07:12:51Z</dc:date>
    </item>
    <item>
      <title>Re: convert character YYYY-mm-dd HH:MM:SS.000000 to date (and overwrite old variables)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/convert-character-YYYY-mm-dd-HH-MM-SS-000000-to-date-and/m-p/250775#M47311</link>
      <description>&lt;P&gt;In order to replace a character variable with a numeric, do this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=oldvar);
set have (rename=(var=oldvar));
format var someformat.;
var = somefunctionof(oldvar);
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You cannot change the type of a variable in place. You need to create a new variable and use the rename option so that it gets the name of the orginal variable.&lt;/P&gt;</description>
      <pubDate>Thu, 18 Feb 2016 07:36:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/convert-character-YYYY-mm-dd-HH-MM-SS-000000-to-date-and/m-p/250775#M47311</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2016-02-18T07:36:04Z</dc:date>
    </item>
    <item>
      <title>Re: convert character YYYY-mm-dd HH:MM:SS.000000 to date (and overwrite old variables)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/convert-character-YYYY-mm-dd-HH-MM-SS-000000-to-date-and/m-p/250796#M47313</link>
      <description>&lt;P&gt;This is a good opportunity&amp;nbsp;to learn a little macro as this task is well suited. &amp;nbsp;You also get to learn how to generate a unique temporary variable name (almost certainly) using and automatic variable SYSINDEX. &amp;nbsp;There are many variations on how to set the default you could for example look at the var name for the a string like DATE/DATETIME and use that to set the defaults. &amp;nbsp;The rest is the same code you already&amp;nbsp;have plus&amp;nbsp;the RENAME and DROP ( the answer to your original question). &amp;nbsp;Getting the LABEL from the old variable to the new variable takes a bit more work.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data data_with_char;
   my_date = '2014-09-01 21:19:45.0000000';
   my_datetime = '2014-09-01 21:19:45.0000000';
   run;


%macro c2date(var,informat,format);
   %if %superq(informat) eq %then %let informat=yymmdd10.;
   %if %superq(format)  eq %then %let format=yymmdd10.;
   format __&amp;amp;sysindex &amp;amp;format;
   informat __&amp;amp;sysindex &amp;amp;informat;
   __&amp;amp;sysindex = input(&amp;amp;var,&amp;amp;informat);
   rename __&amp;amp;sysindex=&amp;amp;var;
   drop &amp;amp;var;
   %mend;
options mprint=1;
data convert;
   set data_with_char;
   %c2date(my_date);
   %c2date(my_datetime,ANYDTDTM19.,Datetime.);
   run;
proc contents varnum;
proc print;
   run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;49         data convert;
50            set data_with_char;
51            %c2date(my_date);
MPRINT(C2DATE):   format __12 yymmdd10.;
MPRINT(C2DATE):   informat __12 yymmdd10.;
MPRINT(C2DATE):   __12 = input(my_date,yymmdd10.);
MPRINT(C2DATE):   rename __12=my_date;
MPRINT(C2DATE):   drop my_date;
52            %c2date(my_datetime,ANYDTDTM19.,Datetime.);
MPRINT(C2DATE):   format __13 Datetime.;
MPRINT(C2DATE):   informat __13 ANYDTDTM19.;
MPRINT(C2DATE):   __13 = input(my_datetime,ANYDTDTM19.);
MPRINT(C2DATE):   rename __13=my_datetime;
MPRINT(C2DATE):   drop my_datetime;
53            run;&lt;/PRE&gt;
&lt;P&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/1926i92089532DA403DF7/image-size/original?v=mpbl-1&amp;amp;px=-1" border="0" alt="Capture.PNG" title="Capture.PNG" /&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 18 Feb 2016 12:01:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/convert-character-YYYY-mm-dd-HH-MM-SS-000000-to-date-and/m-p/250796#M47313</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2016-02-18T12:01:17Z</dc:date>
    </item>
    <item>
      <title>Re: convert character YYYY-mm-dd HH:MM:SS.000000 to date (and overwrite old variables)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/convert-character-YYYY-mm-dd-HH-MM-SS-000000-to-date-and/m-p/251438#M47504</link>
      <description>This macro is really handy - thanks!</description>
      <pubDate>Sun, 21 Feb 2016 22:13:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/convert-character-YYYY-mm-dd-HH-MM-SS-000000-to-date-and/m-p/251438#M47504</guid>
      <dc:creator>mduarte</dc:creator>
      <dc:date>2016-02-21T22:13:00Z</dc:date>
    </item>
  </channel>
</rss>

