0
votes

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

4

4 Answers

0
votes

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;
0
votes

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;
0
votes

Try this:

salesdate_1 = input(put(salesdate,10.),yymmdd10.);

and then add just your format date9.

I always work with this.

0
votes

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.