I have this dataset and i would like to read it. I still encounter some error. May I know how would i be able to solve them?
data ThreeDates;
infile datalines;
input Date1 ddmmmyy10. Date2 ddmmyy10. Date ddmmyy9.;
datalines;
01/03/1950 01/03/1960 03Jan1970
05/15/2000 05/15/2002 15May2003
10/10/1998 11/12/2000 25Dec2005
;
run;
You need to add a : modifier to read non-standard data. It specifies an informat that the INPUT statement uses to read the variable value. The purpose is to read data values that need the additional instructions that informats can provide but that are not aligned in columns. If data was aligned in columns, you would use #n (line pointer controls).
You should also use the date9. informat for the Date column.
data ThreeDates;
infile datalines;
input Date1 :mmddyy10. Date2 :mmddyy10. Date :date9.;
format date: mmddyy10.;
datalines;
01/03/1950 01/03/1960 03Jan1970
05/15/2000 05/15/2002 15May2003
10/10/1998 11/12/2000 25Dec2005
;
run;
More information about lists in INPUT Statement.
PS: Instead of the : modifier, you could also use the INFORMAT statement before the INPUT statement:
data ThreeDates;
infile datalines;
informat date1-date2 mmddyy10. date date9.;
input Date1 Date2 Date;
format date: mmddyy10.;
datalines;
01/03/1950 01/03/1960 03Jan1970
05/15/2000 05/15/2002 15May2003
10/10/1998 11/12/2000 25Dec2005
;
run;
Related
I am trying to read 20 march 2011 date using worddatx32. format but it cant read it gives an error was not found or could not be loaded.
I also try anydtdte32. but it give . in output
how can I read 20 march 2011 date in data lines
data sample;
infile datalines truncover dsd;
input JDate : worddatx32.;
format JDate date9.;
datalines;
20 march 2011
;
run;
WORDDATX. is a FORMAT and you need an INFORMAT. Try using ANYDTDTE. as the informat.
data test ;
input xx anydtdte32. ;
format xx date9.;
put xx= #15 _infile_;
cards;
20 march 2016
20MAR2016
;
Also if you are going to use the : modifier then SAS will stop at the space and only read the 20. Add the & modifier and it will stop when it sees two or more spaces. Of if you are using delimited input then just use an INFORMAT statement.
data test ;
infile cards dsd truncover ;
input xx ;
informat xx anydtdte. ;
format xx date9.;
put xx= #15 _infile_;
cards;
20 march 2016
20MAR2016
;
NOTE: The ANYDTDTE. informat does not support this format before SAS version 9.4
If you cannot use the informat then read the value as three fields and convert it yourself.
input d $ m $ y $ ;
date = input(cats(d,substr(m,1,3),y),date9.);
Not sure why Tom's solution doesn't work on your machine... Here's another possibility:
data mydates;
infile datalines dsd truncover delimiter=" ";
informat day 8.
month $3.
year 8.;
format mydate date9.;
input day month year;
mydate = input(cats(day, month, year),date9.);
datalines;
20 march 2016
18 december 16
;
I want to calculate 'age last birthday' on a specific evaluation date, given a specific date of birth, using a SAS PROC SQL command.
How can I do this and are there any limitations?
Sample Input
DATA INPUTS;
infile cards dlm=',' dsd;
INPUT DOBDt :DATE9. EvalDt :DATE9. expected;
FORMAT DOBDt date9. EvalDt date9.;
CARDS;
11MAY2009,10MAY2015,5
11MAY2009,11MAY2015,6
11MAY2009,12MAY2015,6
28FEB1984,01DEC2015,31
29FEB1984,28FEB2012,27
29FEB1984,29FEB2012,28
29FEB1984,01MAR2012,28
;
RUN;
The goal would be to take the dobDt as an input, evaluate on the EvalDt and produce the answer of expected
This can be done as such :
PROC SQL
PROC SQL;
CREATE TABLE outputs2 AS
select
*
,intck('year',DOBDt,EvalDt,'c') AS actual
,((calculated actual) eq expected) AS check
FROM
inputs
;
QUIT;
actual, the calculated value, matches expected, the desired outcome, for all the examples provided. I am not aware of any limitations to this approach although there are probably some extreme ages that it cannot calculate due to SAS dates having a limited range of values.
As a bonus:
DATA STEP
DATA outputs;
set inputs;
actual = intck('year',DOBDt,EvalDt,'c');
check = (actual eq expected);
RUN;
This is how we used to do it back in the day. Also "age at last birthday" seems pretty clear to me.
DATA INPUTS;
infile cards dlm=',' dsd;
INPUT DOBDt :DATE9. EvalDt :DATE9. expected;
FORMAT DOBDt date9. EvalDt date9.;
age = year(evaldt)-year(dobdt) - (month(evaldt) eq month(dobdt) and day(evaldt) lt day(dobdt)) - (month(evaldt) lt month(dobdt));
CARDS;
11MAY2009,10MAY2015,5
11MAY2009,11MAY2015,6
11MAY2009,12MAY2015,6
28FEB1984,01DEC2015,31
29FEB1984,28FEB2012,27
29FEB1984,29FEB2012,28
29FEB1984,01MAR2012,28
;;;;
RUN;
proc print;
run;
I have a variable that was entered as 31.01.2002 for all entries, and is a character. I would like to put it in numerical form with date9. .
I have tried the below:
date=input(oldway, 10.);
date=input(oldway, date9.);
put date=ddmmyy10.;
date=input(compress(oldway,'.'),10.);
date = INPUT(compress(oldway),date9.);
format date date9.;
run;
I have also tried combinations of the above and to no avail.
Any ideas for forward motion?
Kind Regards!!
You can't input your date using the date9. informat as your string variable isn't in that format. You can use ddmmyy10., though, and that also takes care of the . characters.
data have;
input old $10.;
cards;
31.01.2014
28.02.2014
01.01.2015
;
run;
data want;
set have;
new = input(old, ddmmyy10.);
format new date9.;
run;
try this:
data _null_;
date ="31.01.2014";
date=compress(date,".");
new_date=input(date,ddmmyy8.);
format new_date date9.;
put new_date;
run;
My datalines has 2 variables, date 1 and date 2 with corresponding format ddmmyy10. and mmddyy10.
data date;
input date1 ddmmyy10. date2 mmddyy10.;
datalines;
09/01/2015 01/09/2015
10/01/2015 01/10/2015
11/01/2015 01/11/2015
12/01/2015 01/12/2015
13/01/2015 01/13/2015
;
run;
I tried to add the code but still not work
infile datalines delimiter=' ';
The : (colon) format modifier enables you to use list input but also to specify an informat after a variable name, whether character or numeric. SAS reads until it encounters a blank column, the defined length of the variable (character only), or the end of the data line, whichever comes first.
Format Modifier
data date;
input date1: ddmmyy10. date2: mmddyy10.;
datalines;
09/01/2015 01/09/2015
10/01/2015 01/10/2015
11/01/2015 01/11/2015
12/01/2015 01/12/2015
13/01/2015 01/13/2015
;
run;
The reason why the code is not working is because you are using List input to read non-standard data without colon input modifier or informat statement.
For non-standard data(commas, dollar, date etc. -> Reading Raw data -> Kinds of Data ) or standard data of length greater than 8 byte using List input technique you would need to use either INFORMAT statement or colon modifier with INPUT statement.
1) Assign informat for the input variables using INFORMAT statement or ATTRIB statement
data date;
informat date1 ddmmyy10. date2 mmddyy10.;
input date1 date2;
format date1-date2 yymmdd10.;
datalines;
09/01/2015 01/09/2015
10/01/2015 01/10/2015
;
run;
2) Use Colon (:) input modifier
data date;
input date1: ddmmyy10. date2: mmddyy10.;
format date1-date2 yymmdd10.;
datalines;
09/01/2015 01/09/2015
10/01/2015 01/10/2015
;
run;
I have data in csv format with a certain timestamp field in this format:
' 2009-07-30 20:50:19'
How can I read that into a SS dataset? Ive been trying this, but to no avail.
data filecontents;
infile "C:\es.txt" dlm=',' MISSOVER DSD firstobs=2 lrecl=32767 ;
input START_TIME :ANYDTDTM.
FORMAT START_TIME datetime.
Thanks.
Seems fine to me. The below code works on my machine (9.3 TSM2). What happens for you? Are you just missing a semicolon after the input statement (your example code is)?
data test;
infile "c:\temp\test.csv" dlm=',' missover;
input
dtvar :YMDDTTM.
var1 $
var2 $;
format dtvar DATETIME19.;
put dtvar= DATETIME19.;
run;
result:
608 data test;
609 infile "c:\temp\test.csv" dlm=',' missover;
610 input
611 dtvar :YMDDTTM.
612 var1 $
613 var2 $;
614 format dtvar DATETIME19.;
615 put dtvar= DATETIME19.;
616 run;
NOTE: The infile "c:\temp\test.csv" is:
Filename=c:\temp\test.csv,
RECFM=V,LRECL=256,File Size (bytes)=31,
Last Modified=20Nov2012:20:20:51,
Create Time=20Nov2012:20:17:51
dtvar=30JUL2009:20:50:19
NOTE: 1 record was read from the infile "c:\temp\test.csv".
The minimum record length was 29.
The maximum record length was 29.
NOTE: The data set WORK.TEST has 1 observations and 3 variables.
NOTE: DATA statement used (Total process time):
real time 0.03 seconds
cpu time 0.00 seconds
For what it's worth, YMDDTTMw.d is the specific informat for that (ANYDTDTM. will work as well of course).