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

Hello

User define base month in a structure of YYMM (Macro variable).

Then I want to create automatically 6 macro variables called: Back0,Back1,Back2,Back3,Back4,Back5  which are 0/1/2/3/4/5 months previous months  of base month.

I also want to create automatically 6 macro variables called: month6,month5,month4,month3,month2,month1  which are equal to macro variables Back0,Back1,Back2,Back3,Back4,Back5  (month6 eual to Back0,month5 equal to Back1 and so on).

What is the way to solve the problem with statement k-1?

I know that I can do it in the following way 

%let month6=&back0;
%let month5=&back1;
%let month4=&back2;
%let month3=&back3;
%let month2=&back4;
%let month1=&back5;

But I want to understand what is the problem with k-1 statement and how to solve it?

 

 

%let baseMon=1811;
data tbl;
EndMon = intnx('month',input("&baseMon",yymmn4.),0,'b');/*initial start date*/
startMon = intnx('month',input("&baseMon",yymmn4.),-5,'b');/*End date*/
length YYMM $4;
j=0;
k=6;
do until (startMon > EndMon);
  YYMM = put(EndMon,yymmn4.);
  back=cats('back',j);
  month=cats('month',k);
  call symputx(cats('back',j),YYMM);
  call symputx(cats('month',k),YYMM);
  output;
  j+1;
  k-1;/*Here is the problem. When I write k+1 it works but I need k-1 for my purpose*/
  EndMon = intnx('month',EndMon,-1,'b');
end;
keep YYMM back month;
run;
/**Results that I want to get are:**/
%put &back0;/*1811*/
%put &back1;/*1810*/
%put &back2;/*1809*/
%put &back3;/*1808*/
%put &back4;/*1807*/
%put &back5;/*1806*/

%put &month6;/*1811*/
%put &month5;/*1810*/
%put &month4;/*1809*/
%put &month3;/*1808*/
%put &month2;/*1807*/
%put &month1;/*1806*/

 

1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

@Ronein wrote:

Hello

User define base month in a structure of YYMM (Macro variable).

Then I want to create automatically 6 macro variables called: Back0,Back1,Back2,Back3,Back4,Back5  which are 0/1/2/3/4/5 months previous months  of base month.

I also want to create automatically 6 macro variables called: month6,month5,month4,month3,month2,month1  which are equal to macro variables Back0,Back1,Back2,Back3,Back4,Back5  (month6 eual to Back0,month5 equal to Back1 and so on).

What is the way to solve the problem with statement k-1?

I know that I can do it in the following way 

%let month6=&back0;
%let month5=&back1;
%let month4=&back2;
%let month3=&back3;
%let month2=&back4;
%let month1=&back5;

But I want to understand what is the problem with k-1 statement and how to solve it?

 

 

  k-1;/*Here is the problem. When I write k+1 it works but I need k-1 for my purpose*/

The problem is that the statement

    k+1;

is a summing statement.  Which means that (1) missing k will be treated as a zero, and (2) k will be retained over iterations of the data step.   Since you are doing iterations over a do loop, instead of over multiple data set iterations, you could just as well do k=sum(k,1).

 

But

   k-1;

is NOT such a statement.  But you could do

k=sum(k,-1);

Or if you like the summing statement format you could do   

   k+(-1);

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

View solution in original post

2 REPLIES 2
mkeintz
PROC Star

@Ronein wrote:

Hello

User define base month in a structure of YYMM (Macro variable).

Then I want to create automatically 6 macro variables called: Back0,Back1,Back2,Back3,Back4,Back5  which are 0/1/2/3/4/5 months previous months  of base month.

I also want to create automatically 6 macro variables called: month6,month5,month4,month3,month2,month1  which are equal to macro variables Back0,Back1,Back2,Back3,Back4,Back5  (month6 eual to Back0,month5 equal to Back1 and so on).

What is the way to solve the problem with statement k-1?

I know that I can do it in the following way 

%let month6=&back0;
%let month5=&back1;
%let month4=&back2;
%let month3=&back3;
%let month2=&back4;
%let month1=&back5;

But I want to understand what is the problem with k-1 statement and how to solve it?

 

 

  k-1;/*Here is the problem. When I write k+1 it works but I need k-1 for my purpose*/

The problem is that the statement

    k+1;

is a summing statement.  Which means that (1) missing k will be treated as a zero, and (2) k will be retained over iterations of the data step.   Since you are doing iterations over a do loop, instead of over multiple data set iterations, you could just as well do k=sum(k,1).

 

But

   k-1;

is NOT such a statement.  But you could do

k=sum(k,-1);

Or if you like the summing statement format you could do   

   k+(-1);

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Kurt_Bremser
Super User

Too complicated, build the inversion with a simple subtraction:

%let baseMon=201811;
%let number=6;

data _null_;
do i = 0 to &number.;
  em = put(intnx('month',input("&basemon.",yymmn6.),-i,'b'),yymmn6.);
  call symputx(cats('back',i),em);
  call symputx(cats('month',&number. - i),em);
end;
run;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 2 replies
  • 525 views
  • 1 like
  • 3 in conversation