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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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