I have the following two macro variables:
%let start_date = 29MAY2014;
%let end_date = 15JUL2014;
I would like to create a dataset which is a series of dates between these (inclusive.) I cannot change the input format of the macro variables &start_date and &end_date.
I have tried many variations of the following, but SAS spits out an error for each:
data base_dates;
do date = put("&start_date",date9.) to put("&end_date",date9.);
output;
end;
format date date11.;
run;
Any help in this would be much appreciated
Use them as date literals, enclose in quotes and add a d at the end.
Do date = "&start_date"d to "&end_date"d;
It was simple; input() instead of put()
data base_dates;
do date = input("&start_date",date9.) to input("&end_date",date9.);
output;
end;
format date date11.;
run;
Related
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
I have a column called Last Payment as such
last payment
12DEC09:00:00:00
all the observations follow this structure, I've tried taking a substring such that
data want;
set have;
last_payment=substr(last_payment,1,7);
run;
that doesn't work, I've tried formatting the date with the date7. and date.9 but both just return ********, can someone help me format it into a ddmmmyy ty.
You have to use datapart function before formating it in date7. or date9.
Example:
data new;
format date_new date9. ;
date_new = datepart("12DEC09:00:00:00"dt);
run;
The censored text means wrong format used.
if last_payment is a string then replace "datetime_as_string " with last_payment.
if last_payment is datetime then replace "datetime_as_dt" with last_payment.
The code below will handle the two cases where you are reading the date time as string and numeric values.
Code:
data new;
format datetime_as_dt datetime21. dt_to_date date7. datetime1 date7.;
datetime_as_dt="12DEC09:00:00:00"dt;
datetime_as_string ="12DEC09:00:00:00";
/*Converting datetime to date*/
dt_to_date= datepart(datetime_as_dt);
/*converting datetime string to date*/
/*Option1: Convert string to datetime then extract date*/
datetime1 = datepart(input(datetime_as_string,datetime21.));
/*Option2: Extract date from string then convert to date*/
datetime2 = scan(datetime_as_string,1,':');
put _all_;
run;
Log:
datetime_as_dt=12DEC2009:00:00:00
datetime_as_string=12DEC09:00:00:00
dt_to_date=12DEC09
datetime1=12DEC09
datetime2=12DEC09
Table created:
Parameters start_date, end_date & output_vars. Dates are charter string. How can I convert it in a macro?
data _null_;
start =;
end = ;
diff=end date-startdate;
days = intck('day',start,end);
weeks = intck('week',start,end);
months = intck('month',start,end);
year = intck('year',start,end);
put days= weeks= months= year=;
run;
First if you have macro parameters then they will be macro variables and not data step variables. So you you need to reference the parameter value as &START_DATE, etc.
Second you can use the %SYSFUNC() macro function to call the INTCK() function in macro code.
Third you need to know the date format that will be used by the parameters. It is easiest if you just request that they use DATE format, then you can use the parameter values as date literals.
%let start_date=01JAN2015 ;
%let end_date=01JAN2016 ;
%let days = %sysfunc(intck(day,"&start_date"d,"&end_date"d));
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;
I want to convert a character date variable (categorical) in the format 9/12/1990, 10/1/1990, etc. into this format: 09/12/1990, 10/01/1990, etc. (mmddyy10.) using SAS.
format date_new mmddyy10.;
date_new =input(trim(VAR1),mmddyy10.);
The code is not working.
Try this:
data new;
set old;
/* Parse character date components */
array dt[*] month day year;
do i = 1 to 3;
dt[i] = input(scan(var1, i, "/"), best.);
end;
/* Recontruct date */
date_new = mdy(month, day, year);
format date_new mmddyy10.;
run;
Documentation links:
mdy
scan
SAS dates
Arrays