DATA Step, Macro, Functions and more

how to divide observations by a fixed one

Posts: 0

how to divide observations by a fixed one


I have a problem with a matrice. Could you help?
I have a table that looks like:

name year coefficient week_1 week_2 ...week_n
A Y_1
A Y_2
A Y_3
B Y_1
B Y_2
B Y_3
C Y_1
C Y_2
C Y_3
Z .

I would like to divide all week_i values of Names (A B C) by
week_i values of Name (Z) * coefficient(A B C).

Do you have an idea?
Can I use the lag function in a do-loop to keep the Z row in reference? because I try and it doesn't work...

Many thanks,

Super Contributor
Posts: 260

Re: how to divide observations by a fixed one

Hi Anaïs.
What you have to do is sort your data set to have the Z observation on the very first observation. Then read the dataset with a DATA step, and
RETAIN Zvalue ;
IF _N_=1 THEN Zvalue = value ;
The RETAIN instruction will ensure that the value is remembered for any further observation read.
If there are several values to remember, declare every variable in a single RETAIN, and create them in the IF _N_=1 statement. You can use ARRAYs to simplify syntax, working on week_1, week_2, etc.
Posts: 0

Re: how to divide observations by a fixed one

Hi Olivier,

Many thanks for your reply.
I am not sure to well understand. I have to create new variables for each week_i of my Z(last or first) row?
Here is the macro:

%Macro DUMMY_PERF_HEBDO (table=) ;
data &table.2 (drop=monthly_perf_week_1-monthly_perf_week_500);
set &table ;
array monthly_perf_week_[&_N_week] monthly_perf_week_1-monthly_perf_week_500;
array dummy_week_[500] dummy_week_1-dummy_week_500;
retain monthly_perf_week_1-monthly_perf_week_500 dummy_week_1-dummy_week_500 ;

/*ATTENTION aux 1 premières données week_n*/
do _i=5 to 500;

If (monthly_perf_week_(_i)=0 or monthly_perf_week_(_i)=.) then dummy_week_(_i)=.;
ELSE if BETA =0 then dummy_week_(_i)=.;
Else dummy_week_(_i)=(monthly_perf_week_(_i)-BETA*(monthly_perf_week_(_i)/* THE LAST OR FIRST ROW */ );
%Mend ;

Posts: 0

Re: how to divide observations by a fixed one

Hi olivier,

I've added new variables and it works very well!

The "RETAIN Zvalue ;IF _N_=1 THEN Zvalue = value ;" codes is repeating the value in each cell of the column.

Thanks a lot,

Ask a Question
Discussion stats
  • 3 replies
  • 2 in conversation