## Swapping variable name based on DO IF value.

Moderators: statman, Analyst Techy, andris, Fierce, GerineL, Smash

0101
Posts: 52
Joined: Fri Mar 04, 2011 9:42 pm

### Swapping variable name based on DO IF value.

I have a large series of DO IF / END IF blocks that compute X and Y based on various conditions. It's all wrapped in a larger DO IF that limits the computations to cases where Group = 1.

Code: Select all

``````do if Group = 1.

do if not(missing(StartDate)) and not(missing(EndDate)) and Reason > 0.
compute LengthOfStay = datediff(EndDate,StartDate,"days").
compute ReasonNew = Reason.
end if.

do if not(missing(StartDate)) and (missing(EndDate)) and Reason = 0.
compute LengthOfStay = 888.
compute ReasonNew = 888.
end if.

* repeat for all remaining possible combinations of StateDate, EndDate, and Reason.
...
end if.
execute.``````
I would like to use this same block of syntax for cases where Group = 2. But the syntax for Group 2 should use a MidDate instead of StartDate.

Is there an easy way to edit my syntax to dynamically use StartDate when Group = 1, and use MidDate when Group = 2?
pythonforspss.org
Posts: 116
Joined: Sat Oct 06, 2012 6:21 am

### Re: Swapping variable name based on DO IF value.

Dear 0101,

I'm not sure whether your current approach is the fastest one. Could you please share with us:

1) How many combinations are there altogether?
2) How many different values / formulas do each of the two outcome variables have?
3) Where are the rules that map these on the combinations of conditions? Are they in an ordered file?

Better even if you could share these rules and a (anonimized) sample of your data.
Kind regards,

Ruben Geert van den Berg
http://www.spss-tutorials.com
0101
Posts: 52
Joined: Fri Mar 04, 2011 9:42 pm

### Re: Swapping variable name based on DO IF value.

pythonforspss.org wrote:I'm not sure whether your current approach is the fastest one. Could you please share with us:

1) How many combinations are there altogether?
2) How many different values / formulas do each of the two outcome variables have?
3) Where are the rules that map these on the combinations of conditions? Are they in an ordered file?

Better even if you could share these rules and a (anonimized) sample of your data.
I suspected there was probably a more efficient way to evaluate all of these various conditions. (That was my next challenge to explore!)

1) There are 11 combinations to evaluate. All of them make use of all three variables (EndDate, StartDate, and Reason), connected with AND, to determine what to do. The date variables get evaluated as either missing or not missing, and the Reason variable as either gt 0, 0, or missing:

missing(StartDate)
not(missing(StartDate))
missing(EndDate)
not(missing(EndDate)
Reason gt 0
Reason = 0
missing(Reason)

2) LengthOfStay has three formulas. They are:

LengthOfStay = datediff(EndDate,StartDate,"days"). /* but when Group = 2, MidDate should be used instead of EndDate */.
LengthOfStay = 888. /* still in care */.
LengthOfStay = 999. /* can't calculate - EndDate, StartDate, or both are missing */.

ReasonNew has four formulas:

ReasonNew = Reason
ReasonNew = 777 /* Reason is invalid */.
ReasonNew = 888 /* still in care */.
ReasonNew = 999 /* Reason is missing */.

Also, if (for example) LengthOfStay = 999, this doesn't necessarily mean ReasonNew = 999.

3) I'm not sure what you mean about the location of the rules. Each do if represents a rule (i.e., a combination to evaluate and a value to return). They are not stored anywhere else.
pythonforspss.org
Posts: 116
Joined: Sat Oct 06, 2012 6:21 am

### Re: Swapping variable name based on DO IF value.

Dear 0101,

Due to the complexity of these rules, I don't see any way to automate a solution for this that's much less work than generating them manually.

However, your code looks as if things could be handled with "if" statements instead of "do if".

Code: Select all

``if group=1 and [...] LengthOfStay = datediff(EndDate,StartDate,"days").``
In this case, you'd need nothing more than a single "if" line for each new variable. This "unwrapping" of your code would render the structure of the necessary code very simple and thus more programmable.

If all this is still correct, you could perhaps write out all combinations in Excel with copy-paste and compose a concatenation formula that inserts the combinations of values into the "if" lines. You'd probably copy-paste this block and use the first for all "LengthofStay" values and the second for all "Reasonnew" values. Since "if" statements are transformation commands (see http://pythonforspss.org/transformation.html) this won't cost you processing speed.

Such an Excel sheet would be readable and correctable and doesn't make lapses like we humans do when writing out bunches of code.

Kind regards,

Ruben Geert van den Berg
http://www.spss-tutorials.com

### Who is online

Users browsing this forum: No registered users and 2 guests