<?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: format change by using proc sql based on variable length in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/format-change-by-using-proc-sql-based-on-variable-length/m-p/830353#M328091</link>
    <description>&lt;P&gt;Waste of clock cycles to do this :&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=""&gt; _years=year(input(strip(_datetime),??yymmdd10.));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You have already created a date value so it would make infinitely more sense to use that then to re-convert the character value to date and then extract year (or month or day)&lt;/P&gt;
&lt;PRE&gt;_years = year(_date1);&lt;/PRE&gt;
&lt;P&gt;Testing for length like that only makes sense in the context changing length values in the character value. If you don't want the code to execute when the _datetime is blank(i.e. missing) then test for not missing makes more sense and lets someone reading the code later know why the test is there at all.&lt;/P&gt;</description>
    <pubDate>Thu, 25 Aug 2022 15:15:21 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2022-08-25T15:15:21Z</dc:date>
    <item>
      <title>format change by using proc sql based on variable length</title>
      <link>https://communities.sas.com/t5/SAS-Programming/format-change-by-using-proc-sql-based-on-variable-length/m-p/830296#M328077</link>
      <description>&lt;P&gt;Hello,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need to write following code in proc sql.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Are we able to use when - then conditional statement in proc sql to create similar output?&amp;nbsp;&lt;/P&gt;&lt;P&gt;How can we use strip function in proc sql?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;	data test1;
	  set test2;
	  if length(strip(_datetime)) GE 10 then do;
	  _date1=input(strip(_datetime),??yymmdd10.);
      _years=year(input(strip(_datetime),??yymmdd10.));
      _months=month(input(strip(_datetime),??yymmdd10.));
      _days=day(input(strip(_datetime),??yymmdd10.));
	  end;
     
      if length(strip(_datetime)) LT 9 then do;
      _years=input(scan(_datetime,1),8.);
      _months=input(scan(_datetime,2),8.);
      _days=input(scan(_datetime,3),8.);
	  end;
	  format _date1 yymmdd10.;
	  run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 25 Aug 2022 13:43:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/format-change-by-using-proc-sql-based-on-variable-length/m-p/830296#M328077</guid>
      <dc:creator>dht115</dc:creator>
      <dc:date>2022-08-25T13:43:07Z</dc:date>
    </item>
    <item>
      <title>Re: format change by using proc sql based on variable length</title>
      <link>https://communities.sas.com/t5/SAS-Programming/format-change-by-using-proc-sql-based-on-variable-length/m-p/830298#M328079</link>
      <description>&lt;P&gt;What are some typical values of _datetime? Is it character or numeric? What is the format assigned to _datetime?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Normally it is a poor choice to store calendar dates as a year variable, a month variable and a day variable. Why are you doing this?&lt;/P&gt;</description>
      <pubDate>Thu, 25 Aug 2022 13:52:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/format-change-by-using-proc-sql-based-on-variable-length/m-p/830298#M328079</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-08-25T13:52:58Z</dc:date>
    </item>
    <item>
      <title>Re: format change by using proc sql based on variable length</title>
      <link>https://communities.sas.com/t5/SAS-Programming/format-change-by-using-proc-sql-based-on-variable-length/m-p/830306#M328082</link>
      <description>&lt;P&gt;In SQL you cannot have the DO/END block.&amp;nbsp; So you need to repeat the test for each of the three target variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I will show you how to do the _YEARS and you can replicate for the other two.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;create table test2 as
select *
     , case when (length(strip(_datetime)) GE 10 ) input(strip(_datetime),?yymmdd10.) 
            else . 
       end as _date1 format=yymmdd10.
     , case when (length(strip(_datetime)) GE 10 ) then _years=year(calculated _date1)
            else input(scan(_datetime,1),8.)
       end as _years
 /* repeat for _months and _days */
from test1
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 25 Aug 2022 14:08:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/format-change-by-using-proc-sql-based-on-variable-length/m-p/830306#M328082</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-08-25T14:08:00Z</dc:date>
    </item>
    <item>
      <title>Re: format change by using proc sql based on variable length</title>
      <link>https://communities.sas.com/t5/SAS-Programming/format-change-by-using-proc-sql-based-on-variable-length/m-p/830321#M328083</link>
      <description>&lt;P&gt;_datetime variable is a character variable.&amp;nbsp;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;_datetime $19&lt;/LI&gt;&lt;LI&gt;YYYY-MM-DDTHH:MM:SS&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;Example:&lt;/P&gt;&lt;P&gt;2017-07-20T09:45:27&lt;/P&gt;</description>
      <pubDate>Thu, 25 Aug 2022 14:14:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/format-change-by-using-proc-sql-based-on-variable-length/m-p/830321#M328083</guid>
      <dc:creator>dht115</dc:creator>
      <dc:date>2022-08-25T14:14:50Z</dc:date>
    </item>
    <item>
      <title>Re: format change by using proc sql based on variable length</title>
      <link>https://communities.sas.com/t5/SAS-Programming/format-change-by-using-proc-sql-based-on-variable-length/m-p/830326#M328084</link>
      <description>&lt;P&gt;So why do you need this?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; if length(strip(_datetime)) LT 9 then do;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And please answer my other question: "Normally it is a poor choice to store calendar dates as a year variable, a month variable and a day variable. Why are you doing this?"&lt;/P&gt;</description>
      <pubDate>Thu, 25 Aug 2022 14:20:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/format-change-by-using-proc-sql-based-on-variable-length/m-p/830326#M328084</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-08-25T14:20:31Z</dc:date>
    </item>
    <item>
      <title>Re: format change by using proc sql based on variable length</title>
      <link>https://communities.sas.com/t5/SAS-Programming/format-change-by-using-proc-sql-based-on-variable-length/m-p/830327#M328085</link>
      <description>&lt;P&gt;It is one of the requirement. Do not know the reason behind it &lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 25 Aug 2022 14:21:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/format-change-by-using-proc-sql-based-on-variable-length/m-p/830327#M328085</guid>
      <dc:creator>dht115</dc:creator>
      <dc:date>2022-08-25T14:21:44Z</dc:date>
    </item>
    <item>
      <title>Re: format change by using proc sql based on variable length</title>
      <link>https://communities.sas.com/t5/SAS-Programming/format-change-by-using-proc-sql-based-on-variable-length/m-p/830333#M328087</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/324991"&gt;@dht115&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;It is one of the requirement. Do not know the reason behind it &lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Then I suggest you ask questions, because as I said, it is normally a poor idea to store calendar dates as a year variable, a month variable and a day variable, and my advice to you is to NOT do this at all. Leave dates as dates.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What about this question? I don't see an answer&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;So why do you need this?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;PRE class="language-sas"&gt;&lt;CODE&gt; if length(strip(_datetime)) LT 9 then do;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 25 Aug 2022 14:32:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/format-change-by-using-proc-sql-based-on-variable-length/m-p/830333#M328087</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-08-25T14:32:31Z</dc:date>
    </item>
    <item>
      <title>Re: format change by using proc sql based on variable length</title>
      <link>https://communities.sas.com/t5/SAS-Programming/format-change-by-using-proc-sql-based-on-variable-length/m-p/830353#M328091</link>
      <description>&lt;P&gt;Waste of clock cycles to do this :&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=""&gt; _years=year(input(strip(_datetime),??yymmdd10.));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You have already created a date value so it would make infinitely more sense to use that then to re-convert the character value to date and then extract year (or month or day)&lt;/P&gt;
&lt;PRE&gt;_years = year(_date1);&lt;/PRE&gt;
&lt;P&gt;Testing for length like that only makes sense in the context changing length values in the character value. If you don't want the code to execute when the _datetime is blank(i.e. missing) then test for not missing makes more sense and lets someone reading the code later know why the test is there at all.&lt;/P&gt;</description>
      <pubDate>Thu, 25 Aug 2022 15:15:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/format-change-by-using-proc-sql-based-on-variable-length/m-p/830353#M328091</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-08-25T15:15:21Z</dc:date>
    </item>
    <item>
      <title>Re: format change by using proc sql based on variable length</title>
      <link>https://communities.sas.com/t5/SAS-Programming/format-change-by-using-proc-sql-based-on-variable-length/m-p/830359#M328093</link>
      <description>&lt;P&gt;Hello,&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am receiving following error when I run these code:&amp;nbsp;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;ERROR: Function YEAR requires a numeric expression as argument 1.&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data test1;
input _datetime $19.;
datalines;
2021-11-29T05:30:00
17MAR2012
2022-08
;
run;&lt;BR /&gt;
proc sql;
create table test_3 as
select *, case 
	when (length(strip(_datetime)) GE 10 ) then input(strip(_datetime),?yymmdd10.)
	when (length(strip(_datetime)) EQ 9 ) then input(strip(_datetime),date9.)
       end as _date1 format=yymmdd10.
	, case
	when (length(strip(_datetime)) GE 10 ) then _years=year(_date1)
	end as _years
 from test1
;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 25 Aug 2022 15:28:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/format-change-by-using-proc-sql-based-on-variable-length/m-p/830359#M328093</guid>
      <dc:creator>dht115</dc:creator>
      <dc:date>2022-08-25T15:28:53Z</dc:date>
    </item>
    <item>
      <title>Re: format change by using proc sql based on variable length</title>
      <link>https://communities.sas.com/t5/SAS-Programming/format-change-by-using-proc-sql-based-on-variable-length/m-p/830366#M328096</link>
      <description>&lt;P&gt;If you want to reference a variable you have created as part of the current SELECT statement then you need to add the CALCULATED keyword.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;year(calculated _date1)&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 25 Aug 2022 15:41:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/format-change-by-using-proc-sql-based-on-variable-length/m-p/830366#M328096</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-08-25T15:41:36Z</dc:date>
    </item>
    <item>
      <title>Re: format change by using proc sql based on variable length</title>
      <link>https://communities.sas.com/t5/SAS-Programming/format-change-by-using-proc-sql-based-on-variable-length/m-p/830368#M328097</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/324991"&gt;@dht115&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;_datetime variable is a character variable.&amp;nbsp;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;_datetime $19&lt;/LI&gt;
&lt;LI&gt;YYYY-MM-DDTHH:MM:SS&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;P&gt;2017-07-20T09:45:27&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;So use an INFORMAT that understands that type of a string.&lt;/P&gt;
&lt;P&gt;&lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/leforinforref/p0jd9jaqmxd2ezn1kncmpgfqcpiz.htm" target="_blank"&gt;https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/leforinforref/p0jd9jaqmxd2ezn1kncmpgfqcpiz.htm&lt;/A&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;actual_datetime = input(_datetime,E8601DT19.);
actual_date = datepart(actual_datetime);
format actual_date yymmdd10. actual_datetime datetime19.;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 25 Aug 2022 15:47:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/format-change-by-using-proc-sql-based-on-variable-length/m-p/830368#M328097</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-08-25T15:47:11Z</dc:date>
    </item>
    <item>
      <title>Re: format change by using proc sql based on variable length</title>
      <link>https://communities.sas.com/t5/SAS-Programming/format-change-by-using-proc-sql-based-on-variable-length/m-p/830371#M328099</link>
      <description>&lt;P&gt;Hello,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am getting an error:&amp;nbsp;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;&lt;FONT color="#FF0000"&gt;ERROR: The following columns were not found in the contributing tables: _years.&lt;/FONT&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data test1;
input _datetime $19.;
datalines;
2021-11-29T05:30:00
17MAR2012
2022-08
;
run;

proc sql;
create table test_3 as
select *, case 
	when (length(strip(_datetime)) GE 10 ) then input(strip(_datetime),?yymmdd10.)
	when (length(strip(_datetime)) EQ 9 ) then input(strip(_datetime),date9.)
       end as _date1 format=yymmdd10.
	, case
	when (length(strip(_datetime)) GE 10 ) then _years=year(calculated _date1)
	end as _years
	from test1
;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 25 Aug 2022 16:00:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/format-change-by-using-proc-sql-based-on-variable-length/m-p/830371#M328099</guid>
      <dc:creator>dht115</dc:creator>
      <dc:date>2022-08-25T16:00:50Z</dc:date>
    </item>
    <item>
      <title>Re: format change by using proc sql based on variable length</title>
      <link>https://communities.sas.com/t5/SAS-Programming/format-change-by-using-proc-sql-based-on-variable-length/m-p/830373#M328101</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;when (length(strip(_datetime)) GE 10 ) then _years=year(calculated _date1) end as _years&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;_years is not in data set TEST1, so you can't use it in a THEN clause&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;By the way, I think you want&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;when (length(strip(_datetime)) GE 10 ) then year(calculated _date1) end as _years&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 25 Aug 2022 16:08:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/format-change-by-using-proc-sql-based-on-variable-length/m-p/830373#M328101</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-08-25T16:08:16Z</dc:date>
    </item>
  </channel>
</rss>

