BookmarkSubscribeRSS Feed
deleted_user
Not applicable
Hi,

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,

anais
3 REPLIES 3
Olivier
Pyrite | Level 9
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
[pre]
RETAIN Zvalue ;
IF _N_=1 THEN Zvalue = value ;
[/pre]
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.
Regards.
Olivier
deleted_user
Not applicable
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 */ );
End;
run;
%Mend ;

Thanks,
deleted_user
Not applicable
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,

anais

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 3 replies
  • 1621 views
  • 0 likes
  • 2 in conversation