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).
Related
I have a .csv file which has some flight information. Sample data is shown below.
date|sched_dep_time|dep_time|sched_arr_time|arr_time
1/1/2013|515|517|819|830
The 515 here actually means 5:15Hrs. How can I read this data into SAS correctly? If I use the time. format, it is coming up with some strange timings. I have seen some code snippets, which has to be written exclusively to do these time conversions. But is there are more straight forward method available?
Use the informat HHMMSS, which will read it in correctly.
data have;
informat date ddmmyy10. sched_dep_time dep_time sched_arr_time arr_time hhmmss.;
format sched_dep_time dep_time sched_arr_time arr_time time.;
input date sched_dep_time dep_time sched_arr_time arr_time;
cards;
1/1/2013 515 517 819 830
;
run;
proc print data=have;run;
I didn't realize the HHMMSS. INFORMAT would work. Reeza's answer is best. If you want a custom function, here you go.
options cmplib=work.fns;
proc fcmp outlib=work.fns.time;
function to_time(x);
minutes = mod(x,100);
hour = (x-minutes)/100;
time = hms(hour,minutes,0);
return (time);
endsub;
run;
data test;
format in_val best.
out_time time.;
in_val = 512;
out_time = to_time(in_val);
put in_val out_time;
run;
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 am trying to create a SAS table for keeping descriptions and names of output tables which includes a formatted date inside. However the output includes date unformatted.
My code:
data tablenames;
infile datalines delimiter=',';
input description: $30. sastablename: $30.;
attrib datetoday format=yymmdd6.;
datetoday = date();
mergedtext=catx('_',sastablename,datetoday);
output;
datalines;
Table for Customers,TfC
Table for Sales,TfS
;
The code output gives TfC_20688 for mergedtext variable.
My desired output for mergedtext variable is TfC_160822.
You need to let CATX() know to use the formatted value. Try using the VVALUE() function if your variables are already formatted. Otherwise use the PUT() function to apply the format you want.
data tablenames;
infile datalines delimiter=',';
input description: $30. sastablename: $30.;
attrib datetoday format=yymmddn8.;
datetoday = date();
mergedtext1=catx('_',sastablename,vvalue(datetoday));
mergedtext2=catx('_',sastablename,put(datetoday,yymmddn8.));
datalines;
Table for Customers,TfC
Table for Sales,TfS
;
P.S. Don't use two digit years.
You can use the PUT() function to convert the SAS date in datetoday (the value of which is 20688) to the yymmdd format you want.
43 data tablenames;
44 infile datalines delimiter=',';
45 input description: $30. sastablename: $30.;
46 mergedtext=catx('_',sastablename,put(date(),yymmddn6.));
47 put mergedtext=;
48 output;
49 datalines;
mergedtext=TfC_160822
mergedtext=TfS_160822
NOTE: The data set WORK.TABLENAMES has 2 observations and 3 variables.
This is the story
This is the input file
mukesh,04/04/15,04/06/15,125.00,333.23
vishant,04/05/15,04/07/15,200.00,200
achal,04/06/15,04/08/15,275.00,55.43
this is the import statement that I am using
data datetimedata;
infile fileref dlm=',';
input lastname$ datechkin mmddyy10. datechkout mmddyy10. room_rate equip_cost;
run;
the below is the log which shows success
NOTE: The infile FILEREF is:
Filename=\\VBOXSVR\win_7\SAS\DATA\datetime\datetimedata.csv,
RECFM=V,LRECL=256,File Size (bytes)=688,
Last Modified=13Jun2015:12:08:36,
Create Time=13Jun2015:09:13:09
NOTE: 17 records were read from the infile FILEREF.
The minimum record length was 34.
The maximum record length was 40.
NOTE: The data set WORK.DATETIMEDATA has 17 observations and 5 variables.
NOTE: DATA statement used (Total process time):
real time 0.01 seconds
I have published only 3 observation here.
Now when I print the sas dataset everything works fine except the room_rate variable.
THe output should be 3 digit numbers , but i am getting only the last digit .
Where Am i going wrong !!!
You're mixing input types. When you use list input, you can't specify informats. You either need to specify them using modified list input (add a colon to the informat) or use an informat statement earlier. The following works.
data datetimedata;
infile datalines dlm=',';
input lastname$ datechkin :mmddyy10. datechkout :mmddyy10. room_rate equip_cost;
datalines;
mukesh,04/04/15,04/06/15,125.00,333.23
vishant,04/05/15,04/07/15,200.00,200
achal,04/06/15,04/08/15,275.00,55.43
;;;;
run;
proc print data=datetimedata;
run;
I have the below raw data
1,,35,000
2,100,45,000
and need the below in a dataset
1 . 35000
2 100 45000
this would require both dsd option and using comma. informat.
How to carry this out?
DSD has nothing to do with this - DSD involves input like
1,,"35,000"
2,100,"45,000"
If that is what you have, then you can use the : operator to read it in with the comma informat.
data test;
infile datalines dlm=',' dsd;
input id
num
dollar :comma8.;
datalines;
1,,"35,000"
2,100,"45,000"
;;;;
run;
If you do not have the quotes around the field, then you will need to parse this somehow. One solution is below, which will work as long as the field with commas is the final field.
data test;
infile datalines dlm=',' dsd;
input #;
if countc(_infile_,',') =3 then do;
_commapos = findc(_infile_,',',-1*length(_infile_));
_infile_ = substr(_infile_,1,_commapos-1)||substr(_infile_,_commapos+1);
end;
input id
num
dollar ;
put _all_;
datalines;
1,,35,000
2,100,45,000
;;;;
run;
If the field your potential is in is in a consistent field, but NOT the first one, you can modify the above solution to correct it. If it's in potentially more than one field, you have a much more difficult problem to solve.