<?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: SAS MACRO - How to convert character variables to date variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/SAS-MACRO-How-to-convert-character-variables-to-date-variables/m-p/852869#M337108</link>
    <description>Thank you! BTW, I had to remove the "%str()" part:&lt;BR /&gt;&lt;BR /&gt;%do n = 1 to %sysfunc(countw(&amp;amp;suffix_list));</description>
    <pubDate>Mon, 09 Jan 2023 17:26:49 GMT</pubDate>
    <dc:creator>rchung</dc:creator>
    <dc:date>2023-01-09T17:26:49Z</dc:date>
    <item>
      <title>SAS MACRO - How to convert character variables to date variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-MACRO-How-to-convert-character-variables-to-date-variables/m-p/852576#M337011</link>
      <description>&lt;P&gt;Hello.&lt;/P&gt;&lt;P&gt;I need to convert 26 character variables (type=Char, len=10 and format=$10.), named var_a, var_b, var_c, ... , var_z.&amp;nbsp;Their values all look like this: "2022-12-31"&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need to convert them to date variables&amp;nbsp;without changing their names. In other words, I need the same variables in the same names but of different attributes: type=Num, len=8 and format=YYMMDD10.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What would be the best way to accomplish this?&lt;/P&gt;&lt;P&gt;Thank you so much!&lt;/P&gt;&lt;P&gt;Rakkoo&lt;/P&gt;</description>
      <pubDate>Fri, 06 Jan 2023 22:06:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-MACRO-How-to-convert-character-variables-to-date-variables/m-p/852576#M337011</guid>
      <dc:creator>rchung</dc:creator>
      <dc:date>2023-01-06T22:06:54Z</dc:date>
    </item>
    <item>
      <title>Re: SAS MACRO - How to convert character variables to date variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-MACRO-How-to-convert-character-variables-to-date-variables/m-p/852577#M337012</link>
      <description>&lt;P&gt;Given that you want to use the same names as you already have, you will need to jump through a hoop or two.&amp;nbsp; Here's the logic that you would need to repeat for all 26 variabes:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;temp_a = input(var_a, yymmdd10.);
drop var_a;
rename temp_a = var_a;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Here's how you would add in macro language to carry out the calculations for multiple variables.&amp;nbsp; The example will use 3 variables, but you can add to the list.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro convert (suffix_list=);
   %local suffix n;
   %do n = 1 to %sysfunc(count(&amp;amp;suffix_list));
      %let suffix = %scan(&amp;amp;suffix_list, &amp;amp;n);
      temp_&amp;amp;suffix = input(var_&amp;amp;suffix, yymmdd10.);
      drop var_&amp;amp;suffix;
      rename temp_&amp;amp;suffix = var_&amp;amp;suffix;
   %end;
%mend convert;

data want;
   set have;
   %convert (suffix_list = a b c)
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Sorry, COUNT is the wrong function.&amp;nbsp; I will look it up and find the right one.&lt;/P&gt;</description>
      <pubDate>Fri, 06 Jan 2023 22:16:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-MACRO-How-to-convert-character-variables-to-date-variables/m-p/852577#M337012</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2023-01-06T22:16:43Z</dc:date>
    </item>
    <item>
      <title>Re: SAS MACRO - How to convert character variables to date variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-MACRO-How-to-convert-character-variables-to-date-variables/m-p/852599#M337014</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Normally I would say the best way is to do this when the data is read into SAS using a proper informat.&lt;/P&gt;
&lt;P&gt;Are these data step variables?&lt;/P&gt;
&lt;P&gt;If so the type cannot be changed, you need to do a rename and create new variables in a different data set of the desired type if you want to use the same name. Something like this:&lt;/P&gt;
&lt;PRE&gt;data want;
   set have (rename=(var_a=oldvar_a var_b=oldvar_b var_c= oldvar_c);
   array old (*) oldvar_a oldvar_b oldvar_c;
   array new(*) var_a var_b var_c ;
   do i=1 to dim(old);
       new[i] = input(old[i],yymmdd10.);
   end;
   format var_a var_b var_c yymmdd10.;
   drop i;
run;&lt;/PRE&gt;
&lt;P&gt;I only typed out 3 variables, you would need add yours in the obvious place. The Rename here is used as a data set option and is applied as the data is read from an existing data set so you can use the changed name in the data step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note: if you have a chance when naming variables with anything resembling sequences SAS will work much better with numeric suffixes of 1, 2, 3 instead of A, B, C as there are many places that SAS will use sequenced lists including in the Rename statement or data set option.&lt;/P&gt;
&lt;PRE&gt;data example;
   input x1 x2 x3;
datalines;
1 2 3
;

data renamed;
   set example (rename=( x1-x3= newx1-newx3));
run;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/438219"&gt;@rchung&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hello.&lt;/P&gt;
&lt;P&gt;I need to convert 26 character variables (type=Char, len=10 and format=$10.), named var_a, var_b, var_c, ... , var_z.&amp;nbsp;Their values all look like this: "2022-12-31"&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I need to convert them to date variables&amp;nbsp;without changing their names. In other words, I need the same variables in the same names but of different attributes: type=Num, len=8 and format=YYMMDD10.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What would be the best way to accomplish this?&lt;/P&gt;
&lt;P&gt;Thank you so much!&lt;/P&gt;
&lt;P&gt;Rakkoo&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 06 Jan 2023 22:20:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-MACRO-How-to-convert-character-variables-to-date-variables/m-p/852599#M337014</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-01-06T22:20:09Z</dc:date>
    </item>
    <item>
      <title>Re: SAS MACRO - How to convert character variables to date variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-MACRO-How-to-convert-character-variables-to-date-variables/m-p/852606#M337015</link>
      <description>&lt;P&gt;You are looking for COUNTW().&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%do n = 1 to %sysfunc(countw(&amp;amp;suffix_list,%str( )));&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 06 Jan 2023 22:23:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-MACRO-How-to-convert-character-variables-to-date-variables/m-p/852606#M337015</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-01-06T22:23:06Z</dc:date>
    </item>
    <item>
      <title>Re: SAS MACRO - How to convert character variables to date variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-MACRO-How-to-convert-character-variables-to-date-variables/m-p/852611#M337019</link>
      <description>&lt;P&gt;Thanks, Tom.&amp;nbsp; Yes, COUNTW is correct not COUNT.&lt;/P&gt;</description>
      <pubDate>Fri, 06 Jan 2023 22:29:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-MACRO-How-to-convert-character-variables-to-date-variables/m-p/852611#M337019</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2023-01-06T22:29:29Z</dc:date>
    </item>
    <item>
      <title>Re: SAS MACRO - How to convert character variables to date variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-MACRO-How-to-convert-character-variables-to-date-variables/m-p/852647#M337030</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/438219"&gt;@rchung&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I find it easier to use a Proc SQL step in cases like this. You get full control over the output including variable order, and you don't need to bother with renaming or counting, so the code becomes simpler. The downside is that it is not dynamic. You have to write the full code, so you miss the fun of getting a complex piece of code to work, but you get the job done in a way that everybody can understand and maintain.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  ID = 1;
  Var_a = '2020-04-30';
  Var_b = '2021-08-31';
  Var_c = '2022-12-31';
run;

proc sql;
  create table want as
    select
      ID,
      input(Var_a,yymmdd10.) as Var_a format=YYMMDD10.,
      input(Var_b,yymmdd10.) as Var_b format=YYMMDD10.,
      input(Var_c,yymmdd10.) as Var_c format=YYMMDD10.
    from have;
quit;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 07 Jan 2023 12:21:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-MACRO-How-to-convert-character-variables-to-date-variables/m-p/852647#M337030</guid>
      <dc:creator>ErikLund_Jensen</dc:creator>
      <dc:date>2023-01-07T12:21:31Z</dc:date>
    </item>
    <item>
      <title>Re: SAS MACRO - How to convert character variables to date variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-MACRO-How-to-convert-character-variables-to-date-variables/m-p/852868#M337107</link>
      <description>&lt;P&gt;Hi Astounding,&lt;/P&gt;&lt;P&gt;Your macro works beautifully. But I cannot format those date variables in the same data step. I also failed to include formats in the macro... Now, their frequencies show "169632" instead of "2013-10-01"&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":beaming_face_with_smiling_eyes:"&gt;😁&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Anyway, I like your macro. I have learned a lot from you.&lt;/P&gt;&lt;P&gt;Thank you!&lt;/P&gt;&lt;P&gt;Rakkoo&lt;/P&gt;</description>
      <pubDate>Mon, 09 Jan 2023 17:25:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-MACRO-How-to-convert-character-variables-to-date-variables/m-p/852868#M337107</guid>
      <dc:creator>rchung</dc:creator>
      <dc:date>2023-01-09T17:25:03Z</dc:date>
    </item>
    <item>
      <title>Re: SAS MACRO - How to convert character variables to date variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-MACRO-How-to-convert-character-variables-to-date-variables/m-p/852869#M337108</link>
      <description>Thank you! BTW, I had to remove the "%str()" part:&lt;BR /&gt;&lt;BR /&gt;%do n = 1 to %sysfunc(countw(&amp;amp;suffix_list));</description>
      <pubDate>Mon, 09 Jan 2023 17:26:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-MACRO-How-to-convert-character-variables-to-date-variables/m-p/852869#M337108</guid>
      <dc:creator>rchung</dc:creator>
      <dc:date>2023-01-09T17:26:49Z</dc:date>
    </item>
    <item>
      <title>Re: SAS MACRO - How to convert character variables to date variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-MACRO-How-to-convert-character-variables-to-date-variables/m-p/852872#M337111</link>
      <description>Thank you so much! It works perfect! The output is exactly what I need. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;</description>
      <pubDate>Mon, 09 Jan 2023 17:31:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-MACRO-How-to-convert-character-variables-to-date-variables/m-p/852872#M337111</guid>
      <dc:creator>rchung</dc:creator>
      <dc:date>2023-01-09T17:31:53Z</dc:date>
    </item>
    <item>
      <title>Re: SAS MACRO - How to convert character variables to date variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-MACRO-How-to-convert-character-variables-to-date-variables/m-p/852875#M337113</link>
      <description>&lt;P&gt;If you remove the &lt;FONT face="courier new,courier"&gt;%STR( )&lt;/FONT&gt; part (note the space character) then you are asking COUNTW() to use ANY of the multiple default characters ( &lt;SPAN&gt;&lt;FONT face="courier new,courier"&gt;blank ! $ % &amp;amp; ( ) * + , - . / ; &amp;lt; ^ |&lt;/FONT&gt;&amp;nbsp;&lt;/SPAN&gt;)&amp;nbsp; that it uses to delimit the words instead of just the space character.&amp;nbsp; Depending on the content of the "words" that could lead to an overcount.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 09 Jan 2023 17:39:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-MACRO-How-to-convert-character-variables-to-date-variables/m-p/852875#M337113</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-01-09T17:39:44Z</dc:date>
    </item>
    <item>
      <title>Re: SAS MACRO - How to convert character variables to date variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-MACRO-How-to-convert-character-variables-to-date-variables/m-p/852876#M337114</link>
      <description>It is great to know that proc sql can simply the dropping-and-renaming part so much! This is such a useful tip for me. But I do not accept it as the solution, because it is like a two-step solution: (1) Use proc sql to convert variables to dates, and (2) combine these converted variables back into the original datafile. Anyway, I like this tip so much. Very useful! Thank you. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;</description>
      <pubDate>Mon, 09 Jan 2023 17:44:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-MACRO-How-to-convert-character-variables-to-date-variables/m-p/852876#M337114</guid>
      <dc:creator>rchung</dc:creator>
      <dc:date>2023-01-09T17:44:18Z</dc:date>
    </item>
    <item>
      <title>Re: SAS MACRO - How to convert character variables to date variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-MACRO-How-to-convert-character-variables-to-date-variables/m-p/852882#M337116</link>
      <description>Now I see what I did wrong: I used %STR() instead of %STR( ). I missed a space in the parenthesis! Your suggestion works! Thank you!</description>
      <pubDate>Mon, 09 Jan 2023 17:59:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-MACRO-How-to-convert-character-variables-to-date-variables/m-p/852882#M337116</guid>
      <dc:creator>rchung</dc:creator>
      <dc:date>2023-01-09T17:59:24Z</dc:date>
    </item>
  </channel>
</rss>

