BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
UniversitySas
Quartz | Level 8

Suppose I have 100 columns, sorted by id and time.

 

My cols are conveniently named col1, col2, ..., col100.

 

This seems like a really stupid question, but how would I create a new variable which multiples all the columns, and then adds 10 at the end?

 

I tried using do loops, but it just confused me. I know I can just manually type it out, but 

 

Basically, I want:

new_variable = (col1)x(col2)x...x(col100)+10

 

 

Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

@UniversitySas wrote:

Quick question - what does the **dim(col) do?


dim(col) is the number of elements in the array col, i.e. 100, and ** is the exponentiation operator. The hundredth root of the product (=the geometric mean, if applicable) is raised to the hundredth power to retrieve the product.

 

In this case, however, I may potentially have a final negative value across each row, so I don't think geometric mean will work here.


Are you aware of a work around?


A "final negative value" -- do you mean that only the last factor, col100, might be negative?

If so, you can modify the formula as follows:

new_variable=sign(col100)*geomean(of col1-col99, abs(col100))**100+10;

This would be valid irrespective of the sign of col100, as long as col1, ..., col99 are non-negative.

 

If negative values could occur anywhere, then use a DO loop:

data want;
set have;
array col[100];
new_variable=1;
do _n_=1 to 100;
  new_variable=new_variable*col[_n_];
end;
new_variable+10;
run;

View solution in original post

4 REPLIES 4
FreelanceReinh
Jade | Level 19

Hello @UniversitySas,

 

You can use the GEOMEAN function:

data want;
set have;
array col[100];
new_variable=geomean(of col[*])**dim(col)+10;
run;

provided that none of the factors is negative.

 

Shorter without array:

data want;
set have;
new_variable=geomean(of col1-col100)**100+10;
run;
UniversitySas
Quartz | Level 8

Quick question - what does the **dim(col) do?

In this case, however, I may potentially have a final negative value across each row, so I don't think geometric mean will work here.


Are you aware of a work around?

FreelanceReinh
Jade | Level 19

@UniversitySas wrote:

Quick question - what does the **dim(col) do?


dim(col) is the number of elements in the array col, i.e. 100, and ** is the exponentiation operator. The hundredth root of the product (=the geometric mean, if applicable) is raised to the hundredth power to retrieve the product.

 

In this case, however, I may potentially have a final negative value across each row, so I don't think geometric mean will work here.


Are you aware of a work around?


A "final negative value" -- do you mean that only the last factor, col100, might be negative?

If so, you can modify the formula as follows:

new_variable=sign(col100)*geomean(of col1-col99, abs(col100))**100+10;

This would be valid irrespective of the sign of col100, as long as col1, ..., col99 are non-negative.

 

If negative values could occur anywhere, then use a DO loop:

data want;
set have;
array col[100];
new_variable=1;
do _n_=1 to 100;
  new_variable=new_variable*col[_n_];
end;
new_variable+10;
run;
UniversitySas
Quartz | Level 8
thank you, this works perfectly.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 4 replies
  • 1514 views
  • 2 likes
  • 2 in conversation