<?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: Date and text in the same column in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Date-and-text-in-the-same-column/m-p/672271#M23418</link>
    <description>Thanks so much!</description>
    <pubDate>Sat, 25 Jul 2020 06:10:57 GMT</pubDate>
    <dc:creator>Barkat</dc:creator>
    <dc:date>2020-07-25T06:10:57Z</dc:date>
    <item>
      <title>Date and text in the same column</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Date-and-text-in-the-same-column/m-p/670844#M23330</link>
      <description>&lt;P&gt;I have a dataset that contains both text and date value in&amp;nbsp; one column. When I import the dataset in SAS, that column is imported as char. How do I keep the dates in DATE9. format and convert the texts as missing in SAS?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;identifier&lt;/TD&gt;&lt;TD&gt;effectiveFromTime&lt;/TD&gt;&lt;TD&gt;effectiveThruTime&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;4/23/1987 23:00&lt;/TD&gt;&lt;TD&gt;NULL&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;6/4/1987 0:55&lt;/TD&gt;&lt;TD&gt;7/1/1997 10:50&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;5/27/1987 8:20&lt;/TD&gt;&lt;TD&gt;7/15/2015 11:20&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;5/28/1987 5:20&lt;/TD&gt;&lt;TD&gt;8/4/2011 0:00&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;TD&gt;6/1/1987 14:56&lt;/TD&gt;&lt;TD&gt;NULL&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;6&lt;/TD&gt;&lt;TD&gt;5/28/1987 4:50&lt;/TD&gt;&lt;TD&gt;NULL&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;7&lt;/TD&gt;&lt;TD&gt;4/15/1987 0:20&lt;/TD&gt;&lt;TD&gt;NULL&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;8&lt;/TD&gt;&lt;TD&gt;5/11/1987 17:06&lt;/TD&gt;&lt;TD&gt;9/6/1996 21:30&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;9&lt;/TD&gt;&lt;TD&gt;6/8/1987 10:40&lt;/TD&gt;&lt;TD&gt;7/1/1997 11:50&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 21 Jul 2020 00:11:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Date-and-text-in-the-same-column/m-p/670844#M23330</guid>
      <dc:creator>Barkat</dc:creator>
      <dc:date>2020-07-21T00:11:48Z</dc:date>
    </item>
    <item>
      <title>Re: Date and text in the same column</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Date-and-text-in-the-same-column/m-p/670847#M23331</link>
      <description>&lt;P&gt;Use the SCAN function to get the first part of the text, and the INPUT function with the ?? modifier to convert to a SAS date value or a missing value without issuing a warning:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Identifier   effectiveFromTime :&amp;amp;$20.   effectiveThruTime :&amp;amp;$20.;
datalines;
1   4/23/1987 23:00  NULL
2   6/4/1987 0:55   7/1/1997 10:50
3   5/27/1987 8:20  7/15/2015 11:20
4   5/28/1987 5:20  8/4/2011 0:00
5   6/1/1987 14:56  NULL
6   5/28/1987 4:50  NULL
7   4/15/1987 0:20  NULL
8   5/11/1987 17:06  9/6/1996 21:30
9   6/8/1987 10:40  7/1/1997 11:50
;

data want;
set have;
effectiveFromDate = input(scan(effectiveFromTime, 1, " "), ?? mmddyy.);
effectiveThruDate = input(scan(effectiveThruTime, 1, " "), ?? mmddyy.);
format effectiveFromDate effectiveThruDate date9.;
drop effectiveFromTime effectiveThruTime;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 21 Jul 2020 01:04:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Date-and-text-in-the-same-column/m-p/670847#M23331</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2020-07-21T01:04:03Z</dc:date>
    </item>
    <item>
      <title>Re: Date and text in the same column</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Date-and-text-in-the-same-column/m-p/670856#M23334</link>
      <description>Thanks PGStats. That worked. Could you explain the purpose of "??", please.</description>
      <pubDate>Tue, 21 Jul 2020 01:58:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Date-and-text-in-the-same-column/m-p/670856#M23334</guid>
      <dc:creator>Barkat</dc:creator>
      <dc:date>2020-07-21T01:58:58Z</dc:date>
    </item>
    <item>
      <title>Re: Date and text in the same column</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Date-and-text-in-the-same-column/m-p/670858#M23336</link>
      <description>&lt;P&gt;?? tells SAS not to issue a warning message when a data conversion fails, such as when you try to convert "NULL" into a SAS date.&lt;/P&gt;</description>
      <pubDate>Tue, 21 Jul 2020 02:03:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Date-and-text-in-the-same-column/m-p/670858#M23336</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2020-07-21T02:03:04Z</dc:date>
    </item>
    <item>
      <title>Re: Date and text in the same column</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Date-and-text-in-the-same-column/m-p/672211#M23410</link>
      <description>I am sorry to respond late. I just realized that the want dataset is not giving the correct date. The output dataset is as bellow.&lt;BR /&gt;&lt;BR /&gt;Identifier effectiveFromDate effectiveThruDate&lt;BR /&gt;1 23-Apr-01 .&lt;BR /&gt;2 4-Jun-19 1-Jul-19&lt;BR /&gt;3 27-May-01 15-Jul-02&lt;BR /&gt;4 28-May-01 4-Aug-20&lt;BR /&gt;5 1-Jun-19 .&lt;BR /&gt;6 28-May-01 .&lt;BR /&gt;7 15-Apr-01 .&lt;BR /&gt;8 11-May-01 6-Sep-19&lt;BR /&gt;9 8-Jun-19 1-Jul-19&lt;BR /&gt;</description>
      <pubDate>Fri, 24 Jul 2020 21:26:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Date-and-text-in-the-same-column/m-p/672211#M23410</guid>
      <dc:creator>Barkat</dc:creator>
      <dc:date>2020-07-24T21:26:27Z</dc:date>
    </item>
    <item>
      <title>Re: Date and text in the same column</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Date-and-text-in-the-same-column/m-p/672243#M23412</link>
      <description>&lt;P&gt;Oups. You need to give a length to the informat (the default is 6 - too short)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
effectiveFromDate = input(scan(effectiveFromTime, 1, " "), ?? mmddyy10.);
effectiveThruDate = input(scan(effectiveThruTime, 1, " "), ?? mmddyy10.);
format effectiveFromDate effectiveThruDate date9.;
drop effectiveFromTime effectiveThruTime;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 24 Jul 2020 22:57:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Date-and-text-in-the-same-column/m-p/672243#M23412</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2020-07-24T22:57:23Z</dc:date>
    </item>
    <item>
      <title>Re: Date and text in the same column</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Date-and-text-in-the-same-column/m-p/672247#M23413</link>
      <description>&lt;P&gt;Thanks so much!&lt;BR /&gt;Unfortunately when I import the data from my excel file (attached here) it gives weird value. Could you help me survive, please. I used the following step to import file. I also saved the excel file into csv and imported, but that did not work as well.&lt;/P&gt;&lt;P&gt;proc import datafile="C:\Users\bullah\Desktop\have.xlsx"&lt;BR /&gt;out=have&lt;BR /&gt;dbms=xlsx&lt;BR /&gt;replace;&lt;BR /&gt;getnames=yes;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 24 Jul 2020 23:51:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Date-and-text-in-the-same-column/m-p/672247#M23413</guid>
      <dc:creator>Barkat</dc:creator>
      <dc:date>2020-07-24T23:51:12Z</dc:date>
    </item>
    <item>
      <title>Re: Date and text in the same column</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Date-and-text-in-the-same-column/m-p/672259#M23415</link>
      <description>&lt;P&gt;Columns that contained non numeric values were not recognized as numeric (including dates) during the import process. This is why you have to do some gymnastics, such as:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc import datafile="/..../have.xlsx"
out=have
dbms=xlsx
replace;
getnames=yes;
run;

data want;
set have;
/* This field was recognized as a datetime during the import process. Convert SAS datetime to SAS date */ 
effectiveFromDate = datepart(effectiveFromTime);
/* This field was NOT recognized as a datetime during the import process. Read Windows date value */
WindowsDateValue = input(effectiveThruTime, ?? best.);
/* Convert to SAS date: Correct for reference date difference between Windows and SAS */
if not missing(WindowsDateValue) then effectiveThruDate = WindowsDateValue + '30DEC1899'd;
format effectiveFromDate effectiveThruDate date9.;
drop effectiveFromTime effectiveThruTime WindowsDateValue;
run;

proc print data=want noobs; run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;identifier 	personIdentifier 	closureReasonCode 	effectiveFromDate 	effectiveThruDate
1 	4 	NULL 	23APR1987 	.
2 	6 	NULL 	04JUN1987 	01JUL1997
3 	10 	2268 	27MAY1987 	15JUL2015
4 	13 	2425 	28MAY1987 	04AUG2011
5 	14 	NULL 	01JUN1987 	.
6 	18 	NULL 	28MAY1987 	.
7 	25 	NULL 	15APR1987 	.
8 	27 	NULL 	11MAY1987 	06SEP1996
9 	29 	NULL 	08JUN1987 	01JUL1997
10 	43 	NULL 	23JUN1987 	.
11 	46 	2425 	17NOV1987 	04AUG2011
12 	47 	2268 	11MAY1987 	04AUG2011
13 	48 	2425 	11MAY1987 	04AUG2011
14 	62 	NULL 	12AUG1987 	.
15 	63 	NULL 	30JUL1987 	.
16 	68 	2425 	12JUN1987 	04AUG2011
17 	81 	2572 	02JUN1987 	04AUG2011
18 	86 	2425 	28JUL1987 	04AUG2011
19 	87 	2425 	28JUL1987 	04AUG2011
20 	94 	NULL 	22SEP1987 	.
21 	98 	2425 	20APR1987 	04AUG2011
22 	99 	2140 	11JUN1987 	04AUG2011
23 	110 	NULL 	21AUG1987 	03JUL1997
24 	119 	2425 	22JUN1987 	04AUG2011
25 	121 	NULL 	07JUL1987 	.&lt;/PRE&gt;</description>
      <pubDate>Sat, 25 Jul 2020 03:48:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Date-and-text-in-the-same-column/m-p/672259#M23415</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2020-07-25T03:48:16Z</dc:date>
    </item>
    <item>
      <title>Re: Date and text in the same column</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Date-and-text-in-the-same-column/m-p/672260#M23416</link>
      <description>&lt;P&gt;One of the challenges is that SAS will import/convert the source date string differently if there is or isn't a string in one of the cells.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To ensure SAS always imports the data the same way below code uses &lt;EM&gt;getnames=no&lt;/EM&gt; for Proc Import. This will treat the header row as data and as these headers are always strings will create all SAS columns of type character. This guarantees a stable outcome of Proc Import independent what values you've actually got in the cells (like all Date strings or also come cells with word NULL).&lt;/P&gt;
&lt;P&gt;Now that you've got a stable result from the import into SAS you can write code to post-process your data and create table want.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* import Excel sheet
   - use getnames=no to guarantee that columns always character
*/
proc import 
  out=inter
  datafile='~/test/have.xlsx' 
  dbms=xlsx 
  replace
  ;
  sheet='Sheet1';
  getnames=no;
 run;

proc print data=inter;
run;

/* convert data to desired result */
data want;
  /* Exclude column names: start reading from obs 2 */
  set inter(firstobs=2);
  /* remove NULL string from source data */
  array vars {*} _character_;
  do _i=1 to dim(vars);
    vars[_i]=tranwrd(vars[_i],'NULL',' ');
  end;
  /* convert Excel date values to SAS Date values */
  format effectiveFromTime effectiveThruTime date9.;
  effectiveFromTime=input(d,?? best32.)-21916;
  effectiveThruTime=input(e,?? best32.)-21916;
  /* rename and drop columns to get desired output structure */
  rename 
    a=identifier
    b=personIdentifier
    c=closureReason
    ;
  drop d e _:;
run;

proc print data=want;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 25 Jul 2020 04:18:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Date-and-text-in-the-same-column/m-p/672260#M23416</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2020-07-25T04:18:18Z</dc:date>
    </item>
    <item>
      <title>Re: Date and text in the same column</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Date-and-text-in-the-same-column/m-p/672270#M23417</link>
      <description>Thanks! You are a life saver.</description>
      <pubDate>Sat, 25 Jul 2020 06:08:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Date-and-text-in-the-same-column/m-p/672270#M23417</guid>
      <dc:creator>Barkat</dc:creator>
      <dc:date>2020-07-25T06:08:22Z</dc:date>
    </item>
    <item>
      <title>Re: Date and text in the same column</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Date-and-text-in-the-same-column/m-p/672271#M23418</link>
      <description>Thanks so much!</description>
      <pubDate>Sat, 25 Jul 2020 06:10:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Date-and-text-in-the-same-column/m-p/672271#M23418</guid>
      <dc:creator>Barkat</dc:creator>
      <dc:date>2020-07-25T06:10:57Z</dc:date>
    </item>
  </channel>
</rss>

