Import date from a txt SAS - import

Currently I'm trying to import a .txt file to SAS, but I have one problem. The .txt I recieve has a columnthat looks this way
.......;2015/09/01 09:49;....
I need to import it as a date value not as a string. I've tryed many formats but none of them works correctly.
data aux;
infile "&LIBIN/&fichero" delimiter = ';' MISSOVER DSD lrecl=32767;
format fecha_mov .... ;
informat fecha_mov ....;
input
fecha_mov ;
run;
Thanks for the help in advanced,
Antonio,

The informat anydtdtm. works for me.
https://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a002605552.htm
data want;
infile cards dsd dlm=',';
informat dt anydtdtm.;
format dt datetime21.;
input dt;
cards;
2015/09/01 09:49
2015/09/01 08:49
2015/09/02 09:49
2015/09/05 09:49
;
run;
proc print;
run;

I would suggest using a more specific informat- that way if the source file ever changes unexpectedly, you remove the risk of the anydtdtm. format interpreting it incorrectly. If you use the : format modifier, you can ignore the time part and just use yymmdd10.:
data want;
infile cards dsd dlm=',';
input dt :yymmdd10. another_var $5.;
format dt yymmdd10.;
cards;
2015/09/01 09:49,dummy
2015/09/01 08:49,dummy
2015/09/02 09:49,dummy
2015/09/05 09:49,dummy
;
run;
proc print;
run;
N.B. if you don't use a : then SAS will not move the line pointer to the next delimiter character before starting to read in the next variable.

Related

How to display date as DD MMM YYYY imported from .csv

I have a service that picks up a .csv and processes it to .sas7bdat.
I modify an existing .sas template by filling in variable names, length, and specifying output location.
I have a scenario in which my .csv has a date formatted as 'DD MMM YYYY'. I need to preserve this format to the output file. The predefined formats allow for 'DDMMMYYY' and 'DD-MMM-YYYY'. I have tried these format types in the template.
Below is the template with standard character type:
LIBNAME TempSrc "C:\Temp";
proc import datafile="\\***\FileLocation\file.csv"
out=mydata dbms=dlm replace;
DELIMITER= ",";
getnames=yes;
options ExtendObsCounter=yes;
RUN;
DATA TempSrc.fileName;
attrib DATEVAR length=$11 format=$11. informat=$11. label='Date'
;
set work.mydata;
RUN;
How can I modify this code to output a date as follows '01 JAN 2000'
EDIT:
How can I output this value to the .sas7bdat as a character value?
There isn't a default format to display as DD MMM YYYY so it's necessary to roll your own. I believe the following gives you what you are trying to achieve:
proc format;
picture ddmmmyy
other= '%0d %b %Y' (datatype=date);
run;
data have;
datevar='01 JAN 2000';
run;
data want;
set have;
actualdatevar=input(datevar,anydtdte11.);
format actualdatevar ddmmmyy11.;
run;
gives:
Note that you cannot convert a character variable to numeric, so you may wish to create a new variable and then drop the old one (can also rename the new variable so that you end up with the same named variable).
EDIT1: to apply to specific answer (now situation is clearer)
The following should achieve what is needed:
LIBNAME TempSrc "C:\Temp";
proc format;
picture ddmmmyy
other= '%0d %b %Y' (datatype=date);
run;
proc import datafile="\\***\FileLocation\file.csv"
out=mydata dbms=dlm replace;
DELIMITER= ",";
getnames=yes;
options ExtendObsCounter=yes;
RUN;
/* if all you need to do is apply the format, you may be better using proc datasets */
DATA TempSrc.fileName;
set work.mydata;
format DATEVAR ddmmmyy11. ;
RUN;
EDIT2: converting to character so that it is displayed correctly in universal viewer (without the custom format)
LIBNAME TempSrc "C:\Temp";
proc format;
picture ddmmmyy
other= '%0d %b %Y' (datatype=date);
run;
proc import datafile="\\***\FileLocation\file.csv"
out=mydata dbms=dlm replace;
DELIMITER= ",";
getnames=yes;
options ExtendObsCounter=yes;
RUN;
/* convert numeric to character via rename */
DATA TempSrc.fileName;
set work.mydata (rename=(DATEVAR=DATEVAR2));
DATEVAR=put(DATEVAR2,ddmmmyy11.);
drop DATEVAR2;
RUN;

How To store SAS dates in a macro

I am trying to create different datasets with the names starting from a date which has data for different dates. When i am trying to run the code it is reading the date as numeric numbers and not as Dates. Here is the code
data dates;
input dates mmddyy8. name : $10. ;
format dates date9.;
cards;
01312015 swati
02282015 kangan
01232015 Gotam
04302015 Hushiyar
05172015 yash
09192015 Kuldeep
08302015 David
05172015 yash
11192015 Uninayal
11192015 Uninayal
12032015 sahil
;
data dates;
set dates;
format new date9.;
new=intnx('month', dates, 0, 'e');
run;
proc sql;
select distinct new into : new1 -: new8 from dates;
quit;
%put &new1.;
%macro swati;
%do i= 1 %to 1;
data data_&&new&i.;
set dates;
if new="&&new&i." then output data_&&new&i.;
run;
%end;
%mend;
%swati;
When I try running this code it gives me the error that says it is reading the dates stored in the macro as numbers. How do i make SAS read the dates as just dates only?

sas date format to read yyyymmdd

I have imported a dataset to SAS using Proc import. Now the problem is I can't change the date format in that dataset. In data the date is in YYYYMMDD for sales date, i wanted to change this is as 02Dec2005. Please find the data below. Please find the SAS code for import
DATA:
StoreID SalesDate InvoiceNumber ProductCode qty SalesType Brick
A0110515 20051205 225004 3519671 1 0 1638
proc import out=sample datafile="C:\Users\Vigneshwaran\Desktop\Vignesh\vipin1.txt"
dbms=tab replace;
getnames=yes;
datarow=2;
run;
Thanks and Regards,
V
You have to use a separate step. PROC IMPORT does not allow you to change formats.
PROC DATASETS can be used to change formats (among other things).
proc datasets lib=work nolist;
modify sample;
format SalesDate date9.;
run;
quit;
There can be 2 Solutions based on how your data was imported and what is the attribute of SalesDate column,
/* IF SalesDate is imported as Numeric */
proc datasets lib=work nolist;
modify sample;
format SalesDate date9.;
run;
/* IF SalesDate is imported as Character */
data want;
set sample(rename=(salesdate=sdate));
length SalesDate 8.;
format SalesDate date9.;
SalesDate=input(SDate,yymmdd8.);
drop SDate;
run;
Try this:
salesdate_1 = input(put(salesdate,10.),yymmdd10.);
and then add just your format date9.
I always work with this.
PROC DATASETS can be used to change formats. But PROC IMPORT is going to read 20051205 as an integer number. Interpreted as a DATE value, that would be 20,051,205 days after January 1, 1960. That's more than 20,000 years after 1960. December 5, 2005 is 16775 days after January 1, 1960. So you need to transform the numeric to character then back to numeric.
My suggestion would be to run the PROC IMPORT interactively and save the code. You can then modify the code, adding something like
SaleDate = INPUT(PUT(salesdate,8.),YYMMDD8.) ;
FORMAT SaleDate DATE9. ;
to convert the integer number into a SAS date. If modifying the code isn't possible, either run a data step with the above transformation, or PROC SQL with the same transformation after the IMPORT.
DATA final (RENAME=(saledate=salesdate));
SET sample ;
SaleDate = INPUT(PUT(salesdate,8.),YYMMDD8.) ;
FORMAT SaleDate DATE9. ;
DROP salesdate ;
RUN ;
or
PROC SQL STIMER EXEC ;
CREATE TABLE final AS
SELECT StoreID, INPUT(PUT(salesdate,8.),YYMMDD8.) AS SalesDate,
InvoiceNumber, ProductCode, qty, SalesType, Brick
FROM sample
;
QUIT ;
where the PROC SQL would be followed by DomPazz' PROC DATASETS to change the format to DATE9.

SAS display date from TODAY() function

In SAS I want to reference the date in MMDDYY form but it keeps spitting out the crazy numbers and not in proper format at all!! I think it is doing the UNIX time.....
Here is what I have after importing the dates from an excel file:
date_today = today();
put date_today = mmddyy10.;
RUN;
PROC PRINT;
RUN;
What am I doing wrong>?! please help , thank you so much!!
Try this:
Data foo;
format date_today mmddyy10.;
date_today = today();
run;
proc print data=foo;
run;

SAS - custom date format

I'm trying to read in a date field that looks like:
Mar 20 2013 12:00AM
I am using the following user-defined date format and it is not working.
proc format;
picture mydate other='%MON %0d %Y %0H:%0M %p' (datatype=datetime);
run;
data DATASET;
infile CSVFILE
delimiter = ',' MISSOVER DSD lrecl=32767 firstobs=2 ;
informat TestDate mydate. ;
format TestDate mydate. ;
run;
Can anyone spot what is wrong with this? This is the first time I've needed to use a custom date format and I'm thinking I am missing something small. I am getting the following error:
NOTE: Informat MYDATE was not found or could not be loaded.
I do not believe you can create INformats using Picture; only formats. (INformat = taking a string and converting to a (in this case) date value, format = taking a date value and converting to a string.)
Fortunately, ANYDTDTM. seems to read this in fine. (I changed to 11AM to make sure the time part was okay.)
data test;
input #1 x ANYDTDTM19.;
put x= DATETIME17.;
datalines;
Mar 20 2013 11:00AM
;;;;
run;