SAS: How to create dates out of month field, day field, and year field - date

I have the following script for inputting my data and creating a dataset.
data HotelRooms;
INFILE '/folders/myfolders/Hotel.dat' missover;
input RoomNo 1-4 NumbeGuests 7-8InMonth $ Inday $ InYear $ OutMonth $
OutDay $ OutYear $ UseWireless :$3. DaysUsed RoomType$53-68 RoomRate 69-71;
checkindate=CAT(InMonth,InDay,InYear);
checkoutdate=CAT(OutMonth, OutDay, OutYear);
If UseWireless='YES' then fee=9.95;
run;
proc print data=HotelRooms noobs;
format checkindate MMDDYY10.;
format checkoutdate MM/DD/YY/10.;
Run;
The data loads into the dataset just fine, but when I create a checkin date and a checkout date I run into issues. When I use CAT I cannot add a date format. The print does not print any / or - in the date, only mm dd yyyy. I will need to be able to use the dates for calculations also.
Any help will be appreciated.

Instead of reading your M/D/Y values as characters, read them as numbers and use the mdy() function to create a SAS date from the constituent parts :
data HotelRooms;
INFILE '/folders/myfolders/Hotel.dat' missover;
input RoomNo 1-4 NumbeGuests 7-8
InMonth Inday InYear
OutMonth OutDay OutYear
UseWireless :$3. DaysUsed RoomType $53-68 RoomRate 69-71;
checkindate = mdy(InMonth,InDay,InYear);
checkoutdate = mdy(OutMonth, OutDay, OutYear);
if UseWireless = 'YES' then fee = 9.95;
format checkindate checkoutdate mmddyy10. ;
run;
proc print data=HotelRooms noobs;
run;

Related

SAS date variable

I have a SAS data set A and one of the column is date. 31AUG2010. when I do the following code:
data b;
set a;
newdate=date;
run;
The newdate becomes numeric and 31AUG2010 becomes 18505. How do I solve it?
Apply a format.
See the example below with two different formats applied.
data b;
set a;
newdate=date;
newdate2 = date;
format newdate date9. newdate2 ddmmyy10.;
run;
Dates are just the number of days from 01JAN1960. You need to apply a format. A format doesn't change how the number is stored, just how it is presented to you.
Data a;
set a;
format newdate date9.;
newdate=date;
run;
You have to assign a date format to the new field. Since Date is a numeric field in SAS; SAS assigns the default format which is numeric.
You can assign the format by adding a format statement
data test;
format newdate date9.;
date='31AUG2010'd;
newdate=date;
run;
date has no format assigned but newdate has date9. format assigned.
Output:
newdate=31AUG2010 date=18505

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?

convert numeric date into DATE in SAS Enterprise Guide

I have a date column which dates are showing as numeric, such as "201101", "201203"...
How can I convert these numeric date into format as "Jan2011", "Mar2012" by using SAS enterprise?
Thanks!
You can do it like this:
DATA test;
input date;
CARDS;
201101
201203
;
RUN;
data test2;
set test;
date2 = input(put(date,6.),yymmn6.);
format date2 monyy7.;
run;
Marti Mayo's answer is correct - but just wanted to point out that it only needs the one step:
DATA test;
format date monyy7. ; *<--- Sets the format you want to view (JAN2011) ;
input date :yymmn6.; *<--- Sets the informat of what you read in(201101);
CARDS;
201101
201203
;RUN;

How to convert characters to date in SAS proc sql

I have a column of data named yearmonth stored as characters data. I want to convert this column into SAS date in another column with format dd/mm/yyyy.
for example lets say one of the data is 201201. I want to convert it into 15/01/2012. The problem is I can only use the proc sql and unable to changed the data. Anybody can help me with this problem?
Edit:
This is the one I have done :
INPUT("01/" || substr(t1.Yearmonth, 5,2) ||"/"|| substr(t1.Yearmonth, 1, 4),ddmmyy10.)
Your dataset with character YYYYMM dates:
data input ;
input date $ ;
cards ;
201201
;run ;
You can create a new variable as below:
proc sql ;
create table output as
select date, input(date, yymmn6.)+14 as newdate
from input
;quit ;
You can try the below;
data test;
mydate='20120115';
date_new = input(mydate, ddmmyy10.);
format date_new date10.;
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;