2

I have a data set like this;

DATA work.faminc;
    INPUT famid faminc1-faminc12 ;
CARDS;
1 3281 3413 3114 2500 2700 3500 3114 3319 3514 1282 2434 2818
2 4042 3084 3108 3150 3800 3100 1531 2914 3819 4124 4274 4471
3 6015 6123 6113 6100 6100 6200 6186 6132 3123 4231 6039 6215
;
RUN;

I can create a variable and do some stuff with it like,

%let N=12;

DATA faminc1b;
   SET faminc ;

   ARRAY Afaminc(12) faminc1-faminc12 ;
   ARRAY Ataxinc(&N) taxinc1-taxinc&N ;
   DO month = 1 TO &N;
     Ataxinc(month) = Afaminc(month) * .10 ;
   END;
RUN;

But I also want to divide every family income to the one before it.

The result should be like faminc1/faminc2 - faminc2/faminc3 - faminc3/faminc4...

So main problem is how to use arithmetic (+,-,*,/) operators to the "N" variable which i have created.

When I tried to simply do this, it doesnt work;

%let N=12;

DATA faminc1b;
   SET faminc ;

   ARRAY Afaminc(12) faminc1-faminc12 ;
   ARRAY Afamdiv(&N) famdiv1-famdiv&N ;
   DO month = 1 TO &N+1;
     Afamdiv(month) = faminc&N/faminc&N+1 ;
   END;
RUN;

Thanks for the help.

kutayatesoglu
  • 27
  • 1
  • 7

2 Answers2

2

I am not exactly sure what you want to achieve, so i can only answer your question regarding an operation on a macrovariable, to get your sample working you should put it in a seperate macro, then you can do the eval function on your macrovariable to add 1.

But as far as i can see, you must use month as your loopingvariable and not N, also you have to stop at 11, because you dont have a variable 13 to divide with variable 12.

%let N=12;
%macro calc;
DATA faminc1b;
   SET faminc ;

   ARRAY Afaminc(12) faminc1-faminc12 ;
   ARRAY Afamdiv(&N) famdiv1-famdiv&N ;
   %DO month = 1 %TO %eval(&N-1);
     Afamdiv(&month) = faminc&month/faminc%eval(&month+1) ;
   %END;
RUN;
%mend;
%calc;
kl78
  • 1,628
  • 1
  • 16
  • 26
0

You do not need to use the macro variable for anything other than to define the upper bound on your varaible list.

Everything else you can do with normal SAS code. Use the DIM() function to find the upper bound arrays. Use the arrays in your calculations. Not sure why you are hardcoding one upper bound and using the macro variable for the other, but if they can be different then you need to consider the length of both arrays to find upper bound for your DO loop.

%let N=12;

DATA faminc1b;
   SET faminc ;
   ARRAY Afaminc faminc1-faminc12 ;
   ARRAY Afamdiv famdiv1-famdiv&N ;
   DO month = 1 TO min(dim(afaminc)-1,dim(afamdiv));
     Afamdiv(month) = afaminc(month)/afaminc(month+1) ;
   END;
RUN;
Tom
  • 47,574
  • 2
  • 16
  • 29
  • The only reason why I hardcode one upperbound is because I update some other code I did before, and forgat to change. You are right both upper bounds will be same all the time so I am gonna refer them to macro variable. Your code also Works fine thank you for showing another way to do it. – kutayatesoglu Sep 30 '16 at 12:48