PDA

View Full Version : need help with a code



Paccio
05-28-2012, 07:33 AM
Hello!

I need help with the creation of some dummy variables and I hope some of you can lend me a hand.

I need to capture all the possible combinations of a certain set of variables.

For instance, let's assume I have three variables a, b, c.

I need one dummy equal to 1 when all three variables are positive, zero otherwise.
I need one dummy equal to 1 when all three variables are negative, zero otherwise.
I need one dummy equal to 1 when a and b are positive and c negative, zero otherwise.
I need one dummy equal to 1 when a and c are positive and b negative, zero otherwise.
Etc etc etc...

I don't need to create each dummy individually, since that would take a lot of time (I actually have much more than just three variables).

What I need is some sort of loop which would automatically create a dummy for each possible combination of positive and negative variable a, b and c.

I don't have any idea if this is something possible with VBA, but if it is I would really really appreciate your help in writing this code.

Thank you very much in advance.

Cheers,


Paolo

mikerickson
05-28-2012, 09:36 AM
Consider this approach.
A can be positive, negative or 0. This can be represented by 1, 2, or 0
similarly B

The string "11" would represent the case where both A and B are positive
The string "12" would represent when A is positive and B is negative
"10" represents B positive A zero.

Tis can be extended to any number of variables

Dim V(1 To 5), i As Long
Dim combString As String

V(1) = -8
V(2) = 8
V(3) = 0
V(4) = -3
V(5) = -3

For i = 1 To UBound(V)
combString = combString & ((3 + Sgn(V(i))) Mod 3)
Next i

MsgBox combString: Rem 21022 = neg, pos, zero, neg, neg

Paccio
05-28-2012, 11:53 AM
Thanks a lot man, I will try this out!