Above is the original dataset: actpri.CMP_Final_ATA
I want to create a loop: for each row, I want the product of all values from this row towards the last value of variable Age_To_Age.
Below is my code, which works fine for a small/made-up dataset, but not for my code:
data myData2;
set actpri.CMP_Final_ATA;
array data_array[*] Age_To_Age; /* Assuming "data" is the variable you want to multiply */
retain data_array;
product = 1;
do i = 1 to _n_;
data_array[_N_] = Age_To_Age;
product = product * data_array[i];
end;
output;
drop i;
run;
It gives me error:
Lol, I think I figured it out now.
This following code worked!
data actpri.CMP_Final_ATA_ATU;
set actpri.CMP_Final_ATA;
by descending Period;
array data_array[66] _temporary_; /* Assuming "data" is the variable you want to multiply */
retain data_array;
Age_To_Ultimate = 1;
do i = 1 to _n_;
data_array[_N_] = Age_To_Age;
Age_To_Ultimate = Age_To_Ultimate * data_array[i];
end;
output;
drop i;
run;
array data_array[*] Age_To_Age;
There is absolutely no need to create an array for one variable. However, later _N_ has a value of 2, and so your code refers to data_array[2] and this will fail as there is no second element in the array.
Perhaps if you explained in words what you are trying to do, we might be able to help (as I said, arrays are not needed here).
Lol, I think I figured it out now.
This following code worked!
data actpri.CMP_Final_ATA_ATU;
set actpri.CMP_Final_ATA;
by descending Period;
array data_array[66] _temporary_; /* Assuming "data" is the variable you want to multiply */
retain data_array;
Age_To_Ultimate = 1;
do i = 1 to _n_;
data_array[_N_] = Age_To_Age;
Age_To_Ultimate = Age_To_Ultimate * data_array[i];
end;
output;
drop i;
run;
data have;
do period=1 to 20;
age_to_age=rand('normal', 1, 0.05);
output;
end;
run;
proc sort data=have;
by descending period;
run;
data calcs;
set have nobs=_nobs;
retain running_mult 1;
running_mult=running_mult*age_to_age;
run;
proc sort data=calcs;
by period;
run;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.