BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
SASAna
Quartz | Level 8

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

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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.

View solution in original post

6 REPLIES 6
Astounding
PROC Star

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

SASAna
Quartz | Level 8

&BEG   -  '2016-12-13'

&BEG1 -  '2016-11-14'

 

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

 

Thanks,

Ana

Reeza
Super User

@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? 

Shmuel
Garnet | Level 18

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;

 

Astounding
PROC Star

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.

SASAna
Quartz | Level 8

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

 

Thanks,

Ana

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 1443 views
  • 0 likes
  • 4 in conversation