Hi SAS Users,
I want to get one month back date and tried the below test. as c_date is not in sas date format, i am getting dot (.) in the &beg &beg1. c_Date is charcter format date ('2016-12-14')
data test;
c_date = '2016-12-14';
call symput('dt',strip(input(put(c_date,yymmddn8.),8.)));
call symput('dt1',strip(put(c_date,WORDDATE20.)));
call symputx('beg',"'"||put(intnx('day',c_date,-1,'beg'),yymmdd10.)||"'",'g');
call symputx('beg1',"'"||put(intnx('day',c_date,-30,'end'),yymmdd10.)||"'",'g');
run;
%put &dt &dt1 &beg &beg1;
Thanks,
Ana
To go back 1 day or 31 days, you wouldn't need INTNX at all. Here's one way:
data _null_;
c_date = '2016-12-14';
true_sasdate = input(c_date, yymmdd10.);
call symputx('beg', put(true_sasdate - 1, yymmdd10.));
call symputx('beg1', put(true_sasdate - 31, yymmdd10.));
run;
%let beg = "&beg";
%let beg1 = "&beg1";
I used the separate %LET statements to simplify the transformations in the DATA step. It's just somewhat easier to read this way.
What would you like as the final result for &BEG and &BEG1?
&BEG - '2016-12-13'
&BEG1 - '2016-11-14'
I want the days to be -31 of the c_date.
Thanks,
Ana
@SASAna wrote:
&BEG - '2016-12-13'
&BEG1 - '2016-11-14'
I want the days to be -31 of the c_date.
Thanks,
Ana
March 1 would return a date in January?
You need first to convert the character date into a sas date in order to use intnx function
then use MONTH in order to calculate previous month date.
What did you mean by -30 in beg1 line ? should it not be -1 for previous month ?
I guess yo meant to do:
data test;
c_date = '2016-12-14';
date_n = input(c_date,yymmdd10.);
call symput('dt',strip(put(date_n,yymmddn8.)));
call symput('dt1',strip(put(date_n,WORDDATE20.)));
call symputx('beg',"'"||put(date_n -1,yymmdd10.)||"'",'g');
call symputx('beg1',"'"||put(intnx('MONTH',date_n,-1,'end'),yymmdd10.)||"'",'g');
run;
%put &dt &dt1 &beg &beg1;
To go back 1 day or 31 days, you wouldn't need INTNX at all. Here's one way:
data _null_;
c_date = '2016-12-14';
true_sasdate = input(c_date, yymmdd10.);
call symputx('beg', put(true_sasdate - 1, yymmdd10.));
call symputx('beg1', put(true_sasdate - 31, yymmdd10.));
run;
%let beg = "&beg";
%let beg1 = "&beg1";
I used the separate %LET statements to simplify the transformations in the DATA step. It's just somewhat easier to read this way.
Thanks much. Solution seemed very easy than my complex method.
Thanks,
Ana
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.