I want to convert a String to Date in SAS, I tried:
data _null_;
monyyyy = 'January 2013';
date = input(substr(strip(monyyyy),1,12),YYMMN.);;
put date=YYMMN.;
run;
i want output like :- date = 201301
Try this
data _null_;
dt = 'January 2013';
date = input(cats('01', substr(dt, 1, 3), substr(dt, length(dt)-4)), date9.);
put date= yymmn.;
run;
Related
I need to convert dates in the following forma:
30-giu-18
30-nov-20
......
into:
30JUN2018
30NOV2020
.......
I tried:
data Test;
set input;
mydates = input(myolddates, ddmmyy10.;)
format mydates ddmmyy10.;
run;
It doesn't work. The variable myolddates is character $9.
Can anyone help me please?
Try this
data have;
input myolddates $9.;
datalines;
30-giu-18
30-nov-20
;
options dflang = Italian ;
data want;
set have;
date = input(myolddates, EURDFDE9.);
format date ddmmyy10.;
run;
I am trying to create a query to find number of occurrences in a list in a SAS dataset, for the past 12 Months starting from Last Month
I have created the macro below to be used in my WHERE clause:
%let cur_date = %sysfunc(today(), date9.);
%let pre_date2 = %sysfunc(putn(%sysfunc(intnx(month, %sysfunc(today()), -1, End)),%sysfunc(intnx(month, %sysfunc(today()), -12, End)) date9.)));
%put &pre_date4;
I would appreciate if you can help me with this.
Thanks
You need two macro variables: one for the end of the prior month and one for the first day 12 months prior to last month.
%let last_month = %sysfunc(intnx(month, %sysfunc(today()), -1, E) );
%let last_12_months = %sysfunc(intnx(month, &last_month., -12, B) );
Now you can run your query using between:
where date BETWEEN &last_month. AND &last_12_months.;
Example:
data have;
do i = -36 to 0;
date = intnx('month', today(), i, 'B');
output;
end;
format date date9.;
drop i;
run;
data want;
set have;
where date BETWEEN &last_month. AND &last_12_months.;
run;
Output:
date
01OCT2020
01NOV2020
01DEC2020
01JAN2021
01FEB2021
01MAR2021
01APR2021
01MAY2021
01JUN2021
01JUL2021
01AUG2021
01SEP2021
I am looking to use a factor based on YRMON (YYYY:MM) from a static list of factors that are based on the last 12 months (rolling forward every month)
I have a static dataset that looks this simple (see below):
I basically need to create a format that changes the yrmon to a rolling 13 months that I can stamp on the factor (for calculation needs) based on the yrmon.
yrmon facto
DATE00 1.00000
DATE01 0.99944
DATE02 0.99907
DATE03 0.99907
DATE04 0.99889
DATE05 0.99799
DATE06 0.99659
DATE07 0.99500
DATE08 0.99296
DATE09 0.99100
DATE10 0.85000
DATE11 0.78000
DATE12 0.34900
Example Data before/after:
YRMON PAYAMT PAYAMT_AFTER
2020:10 $100.00 $34.90
2020:08 $100.00 $85.00
Here's what I have so far:
/*Create macro dates*/
%let to_date = %sysfunc(date());
%let paydt = %sysfunc(intnx(month,&to_date, -(0)));
%let payday = %eval(&paydt. -1);
%let chirodt12 = %sysfunc(intnx(month,&payday,-0), date9.);
%let chirodt11 = %sysfunc(intnx(month,&payday,-1), date9.);
%let chirodt10 = %sysfunc(intnx(month,&payday,-2), date9.);
%let chirodt9 = %sysfunc(intnx(month,&payday,-3), date9.);
%let chirodt8 = %sysfunc(intnx(month,&payday,-4), date9.);
%let chirodt7 = %sysfunc(intnx(month,&payday,-5), date9.);
%let chirodt6 = %sysfunc(intnx(month,&payday,-6), date9.);
%let chirodt5 = %sysfunc(intnx(month,&payday,-7), date9.);
%let chirodt4 = %sysfunc(intnx(month,&payday,-8), date9.);
%let chirodt3 = %sysfunc(intnx(month,&payday,-9), date9.);
%let chirodt2 = %sysfunc(intnx(month,&payday,-10), date9.);
%let chirodt1 = %sysfunc(intnx(month,&payday,-11), date9.);
%let chirodt0 = %sysfunc(intnx(month,&payday,-12), date9.);
%put chirodt12 &chirodt12.;
%put chirodt11 &chirodt11.;
%put chirodt10 &chirodt10.;
%put chirodt9 &chirodt9.;
%put chirodt8 &chirodt8.;
%put chirodt7 &chirodt7.;
%put chirodt6 &chirodt6.;
%put chirodt5 &chirodt5.;
%put chirodt4 &chirodt4.;
%put chirodt3 &chirodt3.;
%put chirodt2 &chirodt2.;
%put chirodt1 &chirodt1.;
%put chirodt0 &chirodt0.;
filename ibnr "/path/to/file/file.txt";
data ibnr;
infile ibnr dlm='09'X dsd missover firstobs = 2;
informat month $6. factor 7.5;
input month factor;
run;
data ibnr;
set ibnr;
if month = 'DATE12' then yrmon = put("&chirodt12"D, yymmc7.);
else if month = 'DATE11' then yrmon = put("&chirodt11"D, yymmc7.);
else if month = 'DATE10' then yrmon = put("&chirodt10"D, yymmc7.);
else if month = 'DATE09' then yrmon = put("&chirodt9"D, yymmc7.);
else if month = 'DATE08' then yrmon = put("&chirodt8"D, yymmc7.);
else if month = 'DATE07' then yrmon = put("&chirodt7"D, yymmc7.);
else if month = 'DATE06' then yrmon = put("&chirodt6"D, yymmc7.);
else if month = 'DATE05' then yrmon = put("&chirodt5"D, yymmc7.);
else if month = 'DATE04' then yrmon = put("&chirodt4"D, yymmc7.);
else if month = 'DATE03' then yrmon = put("&chirodt3"D, yymmc7.);
else if month = 'DATE02' then yrmon = put("&chirodt2"D, yymmc7.);
else if month = 'DATE01' then yrmon = put("&chirodt1"D, yymmc7.);
else if month = 'DATE00' then yrmon = put("&chirodt0"D, yymmc7.);
run;
/*format for later use to stamp on factor for calculations*/
data chrofact (keep = fmtname label start type hlo);
retain fmtname 'chfact' type 'C';
set ibnr end = lastrec;
start=yrmon;
label = factor;
output;
if lastrec then do;
start='XXX' ;
hlo = 'O';
label = '0';
output;
end;
run;
proc format cntlin = chrofact;
run;
This gives me what I want; but I'm just sure there's a simpler method out there
Converting a text value to a SAS date value is an INPUT operation (i.e. INFORMAT), not a PUT operation (i.e. FORMAT).
Instead consider creating a lookup table that maps a literal string "DATE<nn>" to a SAS date value corresponding to some predetermined base date.
Example
Decode the <nn> portion of the string literal and apply it in a call to INTNX. Use the output as the basis of CNTLIN for INFORMAT or as a lookup table LEFT JOINED or HASH JOINED to some other data set containing string literals "DATE"
data have;
length yrmon $6 facto 8.;
input yrmon facto;
basedate = today();
month = input(substr(yrmon,5),2.);
month_ago = 12-month;
ago_date = intnx('month', basedate, -month_ago);
format ago_date yymmdd10.;
datalines;
DATE00 1.00000
DATE01 0.99944
DATE02 0.99907
DATE03 0.99907
DATE04 0.99889
DATE05 0.99799
DATE06 0.99659
DATE07 0.99500
DATE08 0.99296
DATE09 0.99100
DATE10 0.85000
DATE11 0.78000
DATE12 0.34900
;
Image of output data set in viewer
I'm looking to take a variable observation's date and essentially keep rolling it forward by its specified repricing parameter until a target date
the dataset being used is:
data have;
input repricing_frequency date_of_last_repricing end_date;
datalines;
3 15399 21367
10 12265 21367
15 13879 21367
;
format date_of_last_repricing end_date date9.;
informat date_of_last_repricing end_date date9.;
run;
so the idea is that i'd keep applying the repricing frequency of either 3 months, 10 months or 15 months to the date_of_last_repricing until it is as close as it can be to the date "31DEC2017". Thanks in advance.
EDIT including my recent workings:
data want;
set have;
repricing_N = intck('Month',date_of_last_repricing,'31DEC2017'd,'continuous');
dateoflastrepricing = intnx('Month',date_of_last_repricing,repricing_N,'E');
format dateoflastrepricing date9.;
informat dateoflastrepricing date9.;
run;
The INTNX function will compute an incremented date value, and allows the resultant interval alignment to be specified (in your case the 'end' of the month n-months hence)
data have;
format date_of_last_repricing end_date date9.;
informat date_of_last_repricing end_date date9.;
* use 12. to read the raw date values in the datalines;
input repricing_frequency date_of_last_repricing: 12. end_date: 12.;
datalines;
3 15399 21367
10 12265 21367
15 13879 21367
;
run;
data want;
set have;
status = 'Original';
output;
* increment and iterate;
date_of_last_repricing = intnx('month',
date_of_last_repricing, repricing_frequency, 'end'
);
do while (date_of_last_repricing <= end_date);
status = 'Computed';
output;
date_of_last_repricing = intnx('month',
date_of_last_repricing, repricing_frequency, 'end'
);
end;
run;
If you want to compute only the nearest end date, as when iterating by repricing frequency, you do not have to iterate. You can divide the months apart by the frequency to get the number of iterations that would have occurred.
data want2;
set have;
nearest_end_month = intnx('month', end_date, 0, 'end');
if nearest_end_month > end_date then nearest_end_month = intnx('month', nearest_end_month, -1, 'end');
months_apart = intck('month', date_of_last_repricing, nearest_end_month);
iterations_apart = floor(months_apart / repricing_frequency);
iteration_months = iterations_apart * repricing_frequency;
nearest_end_date = intnx('month', date_of_last_repricing, iteration_months, 'end');
format nearest: date9.;
run;
proc sql;
select id, max(date_of_last_repricing) as nearest_end_date format=date9. from want group by id;
select id, nearest_end_date from want2;
quit;
Does anyone know how to change a date variable from Date9 to MMDDYY10 format in SAS9.3? I've tried using the put and input functions, but the result is null
Formats are nothing but instructions on how to display a value. Dates are numeric represented as the number of days from 1JAN1960.
data x;
format formated1 date9. formated2 mmddyy10.;
noformated = "01JAN1960"d;
formated1 = noformated;
formated2 = noformated;
run;
proc print data=x;
run;
Obs formated1 formated2 noformated
1 01JAN1960 01/01/1960 0
In short, just change the format on the dataset and the date will be displayed with the new format.
Try both functions:
tmpdate = put(olddate,DATE9.);
newdate = input(tmpdate,MMDDYY10.);
Or maybe even
newdate = input(put(olddate,DATE9.),MMDDYY10.);
For changing the format of variable in a table - PROC SQL or PROC DATASETS:
data WORK.TABLE1;
format DATE1 DATE2 date9.;
DATE1 = today();
DATE2 = DATE1;
run;
proc contents;
run;
proc datasets lib=WORK nodetails nolist;
modify TABLE1;
format DATE1 mmddyy10.;
quit;
proc sql;
alter table WORK.TABLE1
modify DATE2 format=mmddyy10.
;
quit;
proc contents;
run;