## SAS a character date to go one month back

Solved
Frequent Contributor
Posts: 124

# SAS a character date to go one month back

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

Accepted Solutions
Solution
‎12-14-2016 09:53 PM
Super User
Posts: 6,785

## Re: SAS a character date to go one month back

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.

All Replies
Super User
Posts: 6,785

## Re: SAS a character date to go one month back

What would you like as the final result for &BEG and &BEG1?

Frequent Contributor
Posts: 124

## Re: SAS a character date to go one month back

&BEG   -  '2016-12-13'

&BEG1 -  '2016-11-14'

I want the days to be -31 of the c_date.

Thanks,

Ana

Super User
Posts: 23,771

## Re: SAS a character date to go one month back

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?

Posts: 1,837

## Re: SAS a character date to go one month back

[ Edited ]

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;

Solution
‎12-14-2016 09:53 PM
Super User
Posts: 6,785

## Re: SAS a character date to go one month back

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.

Frequent Contributor
Posts: 124

## Re: SAS a character date to go one month back

Thanks much. Solution seemed very easy than my complex method.

Thanks,

Ana

☑ This topic is solved.