<?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 Reassigning variable causing unexpected behavior in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Reassigning-variable-causing-unexpected-behavior/m-p/925921#M364350</link>
    <description>&lt;P&gt;I am trying to update a column as a date with yymmdd10 formatting, but am running into an issue and I am unclear on what is going wrong. Why, in the following code, do variables c and d end up in the correct type and format, but variable a does not?&lt;/P&gt;&lt;P&gt;data test1;&lt;BR /&gt;a = '2016-09-01';&lt;BR /&gt;a = input(a,yymmdd10.);&lt;BR /&gt;b = '2016-09-01';&lt;BR /&gt;c = input(b,yymmdd10.);&lt;BR /&gt;d = input('2016-09-01',yymmdd10.);&lt;BR /&gt;format a b c d yymmdd10.;&lt;BR /&gt;run;&lt;/P&gt;</description>
    <pubDate>Fri, 26 Apr 2024 01:56:37 GMT</pubDate>
    <dc:creator>wh95</dc:creator>
    <dc:date>2024-04-26T01:56:37Z</dc:date>
    <item>
      <title>Reassigning variable causing unexpected behavior</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reassigning-variable-causing-unexpected-behavior/m-p/925921#M364350</link>
      <description>&lt;P&gt;I am trying to update a column as a date with yymmdd10 formatting, but am running into an issue and I am unclear on what is going wrong. Why, in the following code, do variables c and d end up in the correct type and format, but variable a does not?&lt;/P&gt;&lt;P&gt;data test1;&lt;BR /&gt;a = '2016-09-01';&lt;BR /&gt;a = input(a,yymmdd10.);&lt;BR /&gt;b = '2016-09-01';&lt;BR /&gt;c = input(b,yymmdd10.);&lt;BR /&gt;d = input('2016-09-01',yymmdd10.);&lt;BR /&gt;format a b c d yymmdd10.;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Fri, 26 Apr 2024 01:56:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reassigning-variable-causing-unexpected-behavior/m-p/925921#M364350</guid>
      <dc:creator>wh95</dc:creator>
      <dc:date>2024-04-26T01:56:37Z</dc:date>
    </item>
    <item>
      <title>Re: Reassigning variable causing unexpected behavior</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reassigning-variable-causing-unexpected-behavior/m-p/925935#M364352</link>
      <description>&lt;P&gt;Once a variable is numeric, it is always numeric.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Once a variable is character, it is always character.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This line defines the variable A as character:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;a = '2016-09-01';&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;While the next statement works and the INPUT function generates a number, SAS now has to store that number in a character variable.&amp;nbsp; So the result of the INPUT function gets converted back to character.&amp;nbsp; There should be a note on your log about numeric to character conversion taking place at that point.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 26 Apr 2024 03:02:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reassigning-variable-causing-unexpected-behavior/m-p/925935#M364352</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2024-04-26T03:02:55Z</dc:date>
    </item>
    <item>
      <title>Re: Reassigning variable causing unexpected behavior</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reassigning-variable-causing-unexpected-behavior/m-p/925936#M364353</link>
      <description>&lt;P&gt;Based on your code here sample syntax that will populate your variables with SAS Date values.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test1;
  a_char = '2016-09-01';
  a = input(a_char,yymmdd10.);

  b = '01Sep2016'd;

  c = input('2016-09-01',yymmdd10.);
  format a b c yymmdd10.;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 26 Apr 2024 03:10:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reassigning-variable-causing-unexpected-behavior/m-p/925936#M364353</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2024-04-26T03:10:25Z</dc:date>
    </item>
    <item>
      <title>Re: Reassigning variable causing unexpected behavior</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reassigning-variable-causing-unexpected-behavior/m-p/925941#M364354</link>
      <description>&lt;P&gt;That is unfortunate. Thank you for clearing that up. Is there a way to read in a variable as a date immediately? I have a column that has dates in the '2024-04-25' (yyyy-mm-dd) format, but they are being pulled in as character type. Why am I able to convert into a date type from a number,&lt;SPAN&gt;&amp;nbsp;but not from a character?&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 26 Apr 2024 04:17:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reassigning-variable-causing-unexpected-behavior/m-p/925941#M364354</guid>
      <dc:creator>wh95</dc:creator>
      <dc:date>2024-04-26T04:17:50Z</dc:date>
    </item>
    <item>
      <title>Re: Reassigning variable causing unexpected behavior</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reassigning-variable-causing-unexpected-behavior/m-p/925943#M364355</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/465535"&gt;@wh95&lt;/a&gt;&amp;nbsp;You can't change the data type of a variable. In SAS it's either character or numeric. SAS dates are stored in a numeric variable as count of days since 1/1/1960.&lt;/P&gt;
&lt;P&gt;If you create a new variable in SAS then the SAS compiler will define this variable based on the very first occurrence in the data step - so if you assign there a string to variable A then it becomes type character with the length of the string you assign - and after this it's fixed and you can't change it anymore.&lt;/P&gt;
&lt;P&gt;You've got already multiple ways how to convert a character(string) into a SAS date value. Just don't try to then assign this SAS date value to a character variable.&lt;/P&gt;</description>
      <pubDate>Fri, 26 Apr 2024 05:13:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reassigning-variable-causing-unexpected-behavior/m-p/925943#M364355</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2024-04-26T05:13:41Z</dc:date>
    </item>
    <item>
      <title>Re: Reassigning variable causing unexpected behavior</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reassigning-variable-causing-unexpected-behavior/m-p/925968#M364367</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/465535"&gt;@wh95&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;That is unfortunate. Thank you for clearing that up. Is there a way to read in a variable as a date immediately? I have a column that has dates in the '2024-04-25' (yyyy-mm-dd) format, but they are being pulled in as character type. Why am I able to convert into a date type from a number,&lt;SPAN&gt;&amp;nbsp;but not from a character?&lt;/SPAN&gt;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;How are you "pulling in" the values and from what source?&lt;/P&gt;
&lt;P&gt;If a field in an external database is character valued then depending on how you access the source SAS is likely to honor that.&lt;/P&gt;
&lt;P&gt;If you are reading text files you should be able to specify the Informat to read a value:&lt;/P&gt;
&lt;PRE&gt;data mydata;
   infile "&amp;lt;path&amp;gt;\sometextfilesource.txt" &amp;lt;other options&amp;gt;;
  informat datevariable yymmdd10.;
  input datevariable;
run;&lt;/PRE&gt;
&lt;P&gt;SAS has multiple Informats to deal with most common and few less than common date structures.&lt;/P&gt;</description>
      <pubDate>Fri, 26 Apr 2024 09:06:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reassigning-variable-causing-unexpected-behavior/m-p/925968#M364367</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-04-26T09:06:02Z</dc:date>
    </item>
    <item>
      <title>Re: Reassigning variable causing unexpected behavior</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reassigning-variable-causing-unexpected-behavior/m-p/925976#M364370</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/465535"&gt;@wh95&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Is there a way to read in a variable as a date immediately? I have a column that has dates in the '2024-04-25' (yyyy-mm-dd) format, but they are being pulled in as character type.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Pulled in from where? How are you pulling this in? Please give specifics about the source of this data and how you get it into SAS. Don't make us guess, don't make us assume. The more you tell us, the better the answers will be.&lt;/P&gt;</description>
      <pubDate>Fri, 26 Apr 2024 09:49:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reassigning-variable-causing-unexpected-behavior/m-p/925976#M364370</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-04-26T09:49:45Z</dc:date>
    </item>
    <item>
      <title>Re: Reassigning variable causing unexpected behavior</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reassigning-variable-causing-unexpected-behavior/m-p/925980#M364373</link>
      <description>&lt;P&gt;It depends.&amp;nbsp; What is actually in the incoming data?&amp;nbsp; If it contains 10 characters (year, month, day, with dashes in between) then it is simple:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
input datevar yymmdd10.;&lt;BR /&gt;format datevar yymmdd10.;
datalines;
2016-09-01
;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;However, if the incoming data contains quotes, the solution is a little more complex:&lt;/P&gt;
&lt;PRE&gt;data want;
input text $quote12.;
datevar = input(text, yymmdd10.);&lt;BR /&gt;format datevar yymmdd10.;
datalines;
'2016-09-01'
run;&lt;/PRE&gt;
&lt;P&gt;It takes one statement to remove the quotes, then a second statement to convert the remaining text to a date value.&lt;/P&gt;</description>
      <pubDate>Fri, 26 Apr 2024 09:58:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reassigning-variable-causing-unexpected-behavior/m-p/925980#M364373</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2024-04-26T09:58:35Z</dc:date>
    </item>
    <item>
      <title>Re: Reassigning variable causing unexpected behavior</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reassigning-variable-causing-unexpected-behavior/m-p/926023#M364396</link>
      <description>The data is being pulled in from a SQL table. The data in the date column does not have quotes. There is another column with dates formatted as yyyymm.&lt;BR /&gt;&lt;BR /&gt;proc sql;&lt;BR /&gt;CREATE TABLE want AS&lt;BR /&gt;SELECT *&lt;BR /&gt;FROM x.table;&lt;BR /&gt;quit;</description>
      <pubDate>Fri, 26 Apr 2024 14:37:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reassigning-variable-causing-unexpected-behavior/m-p/926023#M364396</guid>
      <dc:creator>wh95</dc:creator>
      <dc:date>2024-04-26T14:37:21Z</dc:date>
    </item>
    <item>
      <title>Re: Reassigning variable causing unexpected behavior</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reassigning-variable-causing-unexpected-behavior/m-p/926028#M364398</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/465535"&gt;@wh95&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;The data is being pulled in from a SQL table. The data in the date column does not have quotes. There is another column with dates formatted as yyyymm.&lt;BR /&gt;&lt;BR /&gt;proc sql;&lt;BR /&gt;CREATE TABLE want AS&lt;BR /&gt;SELECT *&lt;BR /&gt;FROM x.table;&lt;BR /&gt;quit;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Above, you said that the date was&amp;nbsp;&lt;SPAN&gt;'2024-04-25' which is a character string. Please run PROC CONTENTS on x.table and confirm that the date variable is either numeric or text/character. It really doesn't help to tell us what your human eyes see in this case, we need to know what PROC CONTENTS says.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 26 Apr 2024 14:51:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reassigning-variable-causing-unexpected-behavior/m-p/926028#M364398</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-04-26T14:51:21Z</dc:date>
    </item>
    <item>
      <title>Re: Reassigning variable causing unexpected behavior</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reassigning-variable-causing-unexpected-behavior/m-p/926115#M364418</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/465535"&gt;@wh95&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;The data is being pulled in from a SQL table. The data in the date column does not have quotes. There is another column with dates formatted as yyyymm.&lt;BR /&gt;&lt;BR /&gt;proc sql;&lt;BR /&gt;CREATE TABLE want AS&lt;BR /&gt;SELECT *&lt;BR /&gt;FROM x.table;&lt;BR /&gt;quit;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Please share the result from a Proc Contents of this SQL table so we can understand what we're dealing with.&lt;/P&gt;</description>
      <pubDate>Sat, 27 Apr 2024 00:22:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reassigning-variable-causing-unexpected-behavior/m-p/926115#M364418</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2024-04-27T00:22:57Z</dc:date>
    </item>
    <item>
      <title>Re: Reassigning variable causing unexpected behavior</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reassigning-variable-causing-unexpected-behavior/m-p/926159#M364449</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/465535"&gt;@wh95&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;That is unfortunate. Thank you for clearing that up. Is there a way to read in a variable as a date immediately? I have a column that has dates in the '2024-04-25' (yyyy-mm-dd) format, but they are being pulled in as character type. Why am I able to convert into a date type from a number,&lt;SPAN&gt;&amp;nbsp;but not from a character?&lt;/SPAN&gt;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;There is no "date type" in SAS.&amp;nbsp; &lt;STRONG&gt;SAS only has two types of variables&lt;/STRONG&gt;.&amp;nbsp; Floating point numbers and fixed length character strings.&amp;nbsp; Dates are stored as the number of days since 1960.&amp;nbsp; You can attach a format to display those day counts in many different styles depending on your needs. The YYMMDD10. format will display them in that YYYY-MM-DD style.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can convert a number into a date value in the same variable because they both are NUMERIC variables.&amp;nbsp; But you cannot store a date value in a character variable, only a representation of the date as a string, which is what you seem to be starting with.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want to re-use the name of the variable then just add a RENAME statement to change the names back around.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  date_number = input(date,yymmdd10.);
  format date_number yymmdd10.;
  rename date_number=date date=date_character ;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 27 Apr 2024 15:37:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reassigning-variable-causing-unexpected-behavior/m-p/926159#M364449</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-04-27T15:37:54Z</dc:date>
    </item>
  </channel>
</rss>

