BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
yp2609
Fluorite | Level 6

yp2609_0-1686754539912.png

 

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:

yp2609_1-1686754787869.png

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
yp2609
Fluorite | Level 6

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;

View solution in original post

3 REPLIES 3
PaigeMiller
Diamond | Level 26
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).

--
Paige Miller
yp2609
Fluorite | Level 6

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;
Reeza
Super User
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;

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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