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

Hello

User define parameter  of current month :

%let CurMon=1808;

 

SAS should find what is the last month of previous quarter.(and out the value in another parameter called LastQMon)

in this example it should return : LastQMon=1806

 

 

other example:

%let CurMon=1807;

it shoud  return LastQMon=1806

 

other example:

%let CurMon=1805;

it shoud  return LastQMon=1803

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Your problem is here:

Data _null_;
yy=substr(compress(&CurMon.),1,2)-1;
mm=12;
yymm=CAT(yy,mm);
call symput('lastDECMon',yymm);
run;

Since you did not set a specific length for yymm, and used a function, SAS defined it with the default length of 200. So you get 4 digits and 196 spaces.

Either use the trim() function in call symput, or the call symputx() function (which trims automatically), or set a specific length for yymm.

 

See Maxim 47.

View solution in original post

5 REPLIES 5
Kurt_Bremser
Super User

Use the intnx() function, the yymmn4. format to display quarters from a date, and the input function to convert YYMM into a SAS date that I showed you in your other post.

Ronein
Onyx | Level 15

Thanks a lot.

I created the code by your tip.

Now there is another problem.

As you can see in parameter &vector   the last argument is coming with many spaces.

I don't understand why because sas parameter should remove spaces

 

/*Current month*/
%let CurMon=1807;
%put &CurMon;/*1807*/

%let CurMon_date=%sysfunc(inputn(&CurMon.,yymmn4.));
%put &CurMon_date;/*21366*/

/*Previous month*/
%let PrevMon=%sysfunc(intnx(MONTH,&CurMon_date,-1),yymmN4.);
%put &PrevMon;/*1806*/

/*Same month previous year*/
%let SameMonPrevYear=%sysfunc(intnx(MONTH,&CurMon_date,-12),yymmN4.);
%put &SameMonPrevYear;/*1707*/

/*Last DEC*/
Data _null_;
yy=substr(compress(&CurMon.),1,2)-1;
mm=12;
yymm=CAT(yy,mm);
call symput('lastDECMon',yymm);
run;
%put &lastDECMon;/*1712*/

/*Last month in previous qurater*/
data _null_;
f=intnx('quarter',&CurMon_date.,-1);
f2=intnx('quarter',f,0,'e');
f3=put(f2,yymmn4.);
call symput('PrevQ',f3);
format f f2 date9.;
run;
%put &PrevQ;/*1806*/

/*Vector of months*/
%let vector=&CurMon+&PrevMon+&lastDECMon+&PrevQ;
%put &vector; 
Kurt_Bremser
Super User

Your problem is here:

Data _null_;
yy=substr(compress(&CurMon.),1,2)-1;
mm=12;
yymm=CAT(yy,mm);
call symput('lastDECMon',yymm);
run;

Since you did not set a specific length for yymm, and used a function, SAS defined it with the default length of 200. So you get 4 digits and 196 spaces.

Either use the trim() function in call symput, or the call symputx() function (which trims automatically), or set a specific length for yymm.

 

See Maxim 47.

Ronein
Onyx | Level 15

Thank you so so much!

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Why do you not write one post, and state what you want, and what you have.  You have several posts on the same topic here, all of which are not needed.  Macro is not a replacement for Base SAS.

As such that code is virtually indecipherable and not needed.  For instance the post here:

https://communities.sas.com/t5/SAS-Programming/create-date-parameters/m-p/489156

Is exactly the same thing, with a slightly different output.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 2266 views
  • 2 likes
  • 3 in conversation