Quartz | Level 8

how to use array function to perform arithmetic calculation repeatedly on the same dataset

Dear all

Greetings of the day

i have to perform a arithmetical calculation repeated on the same dataset with the repeated variables from the year 2010 to 2018.

my data set is as follows

 Var1_2010 var2_2010 var3_2010 Var1_2011 var2_2011 var3_2011 …………. Var1_2018 var2_2018 var3_2018 21.21 -1.4 -19.8 1.5 5.7 32.1 59.8 21.16 -8.9 5892.48 1807 245.7 824.6 158 7712.8 7617.3 5786.81 2025.8 107.71 -4.6 -18.5 1.2 -1.6 22.6 4.9 -5.9 206.18 35.6 1.5 79.9 19.5 1082 651.6 235.06 51.4 1080.4 -422.2 113.9 158.7 28.6 845.4 1885.2 1103.55 -755.9

I have to do the computation as follows

newvar_2010=var1_2010*0.345+var2_2010*0.657+var3*0.768

i have to repeat it for all the remaining years in the same way  from 2011 to 2018.

please suggest me a SAS code to do the calculation as mentioned above.

1 ACCEPTED SOLUTION

Accepted Solutions
Tourmaline | Level 20

Re: how to use array function to perform arithmetic calculation repeatedly on the same dataset

Untested coz I was lazy to create sample data

``````
data want;
set have;
array _constant(3) _temporary_ (0.345,0.657,0.768) ;
array new(2010:2018) new2010-new2018;
array t(2010:2018,3) var1_2010--var3_2018;
do _i=2010 to 2018;
do _j=1 to 3;
new(_i)=sum(t(_i,_j)*_constant(_j),new(_i));
end;
end;
drop _:;
run;``````
3 REPLIES 3
Super User

Re: how to use array function to perform arithmetic calculation repeatedly on the same dataset

Same answer as your other question really, use an array to loop.

https://stats.idre.ucla.edu/sas/seminars/sas-arrays/arrays-in-sas/
Tourmaline | Level 20

Re: how to use array function to perform arithmetic calculation repeatedly on the same dataset

Untested coz I was lazy to create sample data

``````
data want;
set have;
array _constant(3) _temporary_ (0.345,0.657,0.768) ;
array new(2010:2018) new2010-new2018;
array t(2010:2018,3) var1_2010--var3_2018;
do _i=2010 to 2018;
do _j=1 to 3;
new(_i)=sum(t(_i,_j)*_constant(_j),new(_i));
end;
end;
drop _:;
run;``````
Diamond | Level 26

Re: how to use array function to perform arithmetic calculation repeatedly on the same dataset

Dear all

Greetings of the day

i have to perform a arithmetical calculation repeated on the same dataset with the repeated variables from the year 2010 to 2018.

my data set is as follows

 Var1_2010 var2_2010 var3_2010 Var1_2011 var2_2011 var3_2011 …………. Var1_2018 var2_2018 var3_2018 21.21 -1.4 -19.8 1.5 5.7 32.1 59.8 21.16 -8.9 5892.48 1807 245.7 824.6 158 7712.8 7617.3 5786.81 2025.8 107.71 -4.6 -18.5 1.2 -1.6 22.6 4.9 -5.9 206.18 35.6 1.5 79.9 19.5 1082 651.6 235.06 51.4 1080.4 -422.2 113.9 158.7 28.6 845.4 1885.2 1103.55 -755.9

Adding to the other replies ... this data structure is not a good one for most types of analyses, if you have created this data structure, the next time you might want to make it look like this:

Row VAR YEAR Value

1 var1 2010 21.21

1 var2 2010 -1.4

1 var3 2010 -19.8

2 var1 2010 5892.48

etc.

and the whole analysis of computing sums is extremely simple in PROC SUMMARY.

I realize that many times our data comes from Excel, and there it is organized as you showed in your original post, but if you are creating this data yourself, don't do it as in the original post, do it as I showed in this post.

--
Paige Miller
Discussion stats
• 3 replies
• 465 views
• 1 like
• 4 in conversation