BookmarkSubscribeRSS Feed
HeatherNewton
Quartz | Level 8
If i read text in say 2022-11-30 and i like to format to 30NOV2030, do i just add format date9. after the input statement say
@002 startdate date9.
@010 endate date9.
@018 fruit $8.
12 REPLIES 12
PaigeMiller
Diamond | Level 26

@HeatherNewton wrote:
If i read text in say 2022-11-30 and i like to format to 30NOV2030, do i just add format date9. after the input statement say
@002 startdate date9.
@010 endate date9.
@018 fruit $8.

Please try it and see (Maxim 4).

 

Also, please check for typos and correct them before you send your message, 2022-11-30 is not the same date as  30NOV2030

 

--
Paige Miller
Kurt_Bremser
Super User

The INPUT statement uses informats. Informats control how data is read, not how it is displayed. For the display, use formats in a FORMAT statement.

So, in your INPUT, you have to use an informat which matches the way the dates come in. In a FORMAT statement, assign formats according to how you want the dates to look for you and other users.

The informat for a date in YYYY-MM-DD notation is YYMMDD10. or E8601DA10.

HeatherNewton
Quartz | Level 8
Can i put
Input @002 statedate $10.
@009 enddate $10.

Then later in a dara step put
Data test;
Set test0:
Format startdate date9.;
Format enddate date9.;
Run;
Kurt_Bremser
Super User

NO.

SAS dates are numeric, counting the days from 1960-01-01. So you MUST read date values with the proper numeric informat, as I have already explained. Only then can you use any of the date formats.

 

Tom
Super User Tom
Super User

@HeatherNewton wrote:
Can i put
Input @002 statedate $10.
@009 enddate $10.

Then later in a dara step put
Data test;
Set test0:
Format startdate date9.;
Format enddate date9.;
Run;

If you read the string using the $ informat then you will create CHARACTER variables.  You cannot use a numeric format like DATE with a character variable.  You will first have to make a new numeric variable by using the INPUT() function to convert the character strings into date values.

data test;
  set test0:
  startdate_num = input(startdate,yymmdd10.);
  enddate_num = input(enddate,yymmdd10.);
  format startdate_num enddate_num date9.;
run;

If you are happy storing dates as character (perhaps you don't need to ever use them in comparisons or filtering; you only plan printing them) then since your character variables are of length $10 they are long enough to store the date strings in the ddMONyyyy style generated by DATE9.  (But not long enough to store the dd-MON-yyyy style generated by DATE11.)

data test;
  set test0:
  startdate = put(input(startdate,yymmdd10.),date9.);
  enddate = put(input(enddate,yymmdd10.),date9.);
run;
HeatherNewton
Quartz | Level 8
data test;
  set test0:
  startdate = put(input(startdate,yymmdd10.),date9.);
  enddate = put(input(enddate,yymmdd10.),date9.);
run;

what if I want to use different name here, how to rename them back to original?

data test;

set test0;

startdate1=input(statedate,yymmdd10.);

format startdate1 date9.;

run;

but actually I want startdate to be startdate1

so possible to write like the below:

 

data test (drop startdate1);

set test0;

startdate1=input(statedate,yymmdd10.);

format startdate1 date9.;

rename startdate1=startdate;

run;

 

 

Tom
Super User Tom
Super User

Test it and see.

329  data test (drop=name1);
330    set sashelp.class;
331    name1 = name;
332    rename name1=name;
333  run;

WARNING: Variable name1 cannot be renamed to name because name already exists.
NOTE: There were 19 observations read from the data set SASHELP.CLASS.
NOTE: The data set WORK.TEST has 19 observations and 5 variables.
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.00 seconds

You cannot name TESTDATE1 as TESTDATE because there is already a TESTDATE variable.  You will need to either RENAME that variable also.  Or DROP it.

334  data test ;
335    set sashelp.class;
336    name1 = name;
337    rename name1=name;
338    drop name;
339  run;

NOTE: There were 19 observations read from the data set SASHELP.CLASS.
NOTE: The data set WORK.TEST has 19 observations and 5 variables.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds

I have never had much use for using DROP=,RENAME=,KEEP= dataset options on the OUTPUT dataset for a simple data step that is only creating one dataset.  Seems much clearer to just use the actual DROP, RENAME or KEEP statements instead.

 

Using it on the INPUT dataset makes more sense.

345  data test ;
346    set sashelp.class(rename=(name=old_name));
347    name = old_name;
348    drop old_name;
349  run;

NOTE: There were 19 observations read from the data set SASHELP.CLASS.
NOTE: The data set WORK.TEST has 19 observations and 5 variables.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds

 

HeatherNewton
Quartz | Level 8
Thanks! Got it now. If i have many variable like name that i need to first rename and assign, how to add in rename statement in the set dataset line..
Reeza
Super User
Or just read it in correctly the first time since you are doing the input step.
rvikram
Fluorite | Level 6

Did you actually mean 2020-11-30 to read as 30NOV2030 because that is a little challenging. 

But here is what I would do to get the correct date format. 'd first input the text date as yymmdd10, and then change it to date9.:

 

data test;
input date yymmdd10.;
infile datalines;
datalines;
2020-11-30
;
 
data test2;
set test;
fmt_Date = put(date, date9.);
run;
 
 
Reeza
Super User
@002 startdate yymmdd10.
@010 endate  yymmdd10.
@018 fruit $8. 
....
;

format startdate enddate date9.;

Something along those lines is what you need.

 

The informats in the input statement need to match what the data looks like when read in. 

 

To control what it looks like after, you can use a format statement. This can be in the same data step when the data is read or separate data step, I recommend the import data step if you want the format attached to the data. It can also be modified using proc datasets as well. 

 


@HeatherNewton wrote:
If i read text in say 2022-11-30 and i like to format to 30NOV2030, do i just add format date9. after the input statement say
@002 startdate date9.
@010 endate date9.
@018 fruit $8.

 

Tom
Super User Tom
Super User

Yes, if you want the dates to display in the style ddMMMyyyy then add a FORMAT statement to attach the format specification of DATE9. to the variables after the INPUT statement.

 

But that is NOT what the code fragment you showed is doing. You appear to have modified the INPUT statement itself and changed the INFORMAT to use to read the strings in the text into date values.  Strings that look like YYYY-MM-DD cannot be read using the DATE informat.  You need to use the YYMMDD10. informat to read in string of length 10 with the date parts as numbers in Year Month_of_year Day_of_month order.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 12 replies
  • 1231 views
  • 2 likes
  • 6 in conversation