The DHS Program User Forum
Discussions regarding The DHS Program data and results
Home » Data » Dataset use in Stata » Too long command in Stata?
Too long command in Stata? [message #11605] Sun, 15 January 2017 08:56 Go to next message
nwegbus is currently offline  nwegbus
Messages: 15
Registered: December 2015
Member
Hello,

I'm a doctoral student writing my dissertation using the Nigeria DHS 2013 and Stata 13. I'm trying to construct a variable -achild- (actual number of children) using 4 observed variables in the dataset -v202, v203, v204 and v205- which represent "sons at home, daughters at home, sons elsewhere and daughters elsewhere' respectively.

I'm doing this by generating my new variable and then doing a series of "replace ifs". Since some women have as many as 11 children in any of the four categories, my commands are extremely long and this is causing Stata to return this message:

"error . . . . . . . . . . . . . . . . . . . . . . . . Return code 130
expression too long;
too many SUMs;
In the first case, you specified an expression that is too long
for Stata to process -- the expression contains more than 249
pairs of nested parentheses or more than 800 dyadic operators.
(For Small Stata, the limit is 66 dyadic operators.) Break
the expression into smaller parts. In the second case, the
expression contains more than 5 sum() functions. This
expression, too, will have to be broken into smaller parts."

****Edited to add****
I have already tried using the "///" and "*/" symbols and breaking up the command into seven shorter lines instead of just four. It's still not working.

I'm convinced there is a more efficient way of doing what I'm trying to do in a shorter time, and using less lengthy commands. I just can't seem to figure it out.

Any guidance and suggestions/links would be highly appreciated.

Thank you
SN

[Updated on: Tue, 17 January 2017 15:22]

Report message to a moderator

Re: Too long command in Stata? [message #11622 is a reply to message #11605] Tue, 17 January 2017 17:06 Go to previous messageGo to next message
Bridgette-DHS is currently offline  Bridgette-DHS
Messages: 3190
Registered: February 2013
Senior Member
Following is a response from Senior DHS Stata Specialist, Tom Pullum:

The way to deal with long lines is with #delimit. (Enter "help delimit".) Here's an example of how you change to a long line with a semicolon an the delimiter and then back to regular lines in a program:

#delimit ;
Enter your long line here
;
#delimit cr

You can have multiple lines between the two "#delimit" statements, but there must be a semicolon at the end of, or after, each such line.

The normal delimiter is an end of line mark, which is not visible in a text file, but is referred to as "cr" for "carriage return", harking back to the days of typewriters.


[Updated on: Thu, 19 January 2017 11:34]

Report message to a moderator

Re: Too long command in Stata? [message #11635 is a reply to message #11622] Thu, 19 January 2017 11:09 Go to previous messageGo to next message
nwegbus is currently offline  nwegbus
Messages: 15
Registered: December 2015
Member
Thank you for your response. So based on what I understood you to be saying, the following is what I did:

#delimit ;
replace achild=9 if v202==9 & v203==0 & v204==0 & v205==0| v202==8 & v203==1 & v204==0 & v205==0| v202==8 & v203==0 & v204==1 & v205==0| v202==8 & v203==0 &v204==0 & v205==1| v202==7 & v203==2 & v204==0 & v205==0| v202==7 & v203==0 & v204==2 & v205==0| v202==7 & v203==0 & v204==0 & v205==2| v202==7 & v203==1 & v204==1 & v205==0| v202==7 & v203==0 & v204==1 & v205==1| v202==7 & v203==1 & v204==0 & v205==1| v202==6 & v203==3 & v204==0 & v205==0| v202==6 & v203==0 & v204==3 & v205==0| v202==6 & v203==0 & v204==0 & v205==3| v202==6 & v203==2 & v204==1 & v205==0| v202==6 & v203==0 & v204==2 & v205==1
| v202==6 & v203==1 & v204==0 & v205==2| v202==6 & v203==1 & v204==1 & v205==1| v202==5 & v203==4 & v204==0 & v205==0| v202==5 & v203==0 & v204==4 & v205==0| v202==5 & v203==0 & v204==0 & v205==4| v202==5 & v203==3 & v204==1 & v205==0| v202==5 & v203==0 & v204==3 & v205==1| v202==5 & v203==1 & v204==0 & v205==3
| v202==5 & v203==2 & v204==1 & v205==1| v202==5 & v203==1 & v204==2 & v205==1| v202==5 & v203==1 & v204==1 & v205==2| v202==5 & v203==2 & v204==2 & v205==0| v202==5 & v203==0 & v204==2 & v205==2| v202==5 & v203==2 & v204==0 & v205==2| v202==4 & v203==3 & v204==1 & v205==1| v202==4 & v203==1 & v204==3 & v205==1| v202==4 & v203==1 & v204==1 & v205==3| v202==4 & v203==1 & v204==2 & v205==2| v202==4 & v203==2 & v204==1 & v205==2| v202==4 & v203==2 & v204==2 & v205==1| v202==3 & v203==3 & v204==3 & v205==0| v202==3 & v203==0 & v204==3 & v205==3| v202==3 & v203==3 & v204==0 & v205==3
| v203==9 & v202==0 & v204==0 & v205==0| v203==8 & v202==1 & v204==0 & v205==0| v203==8 & v202==0 & v204==1 & v205==0| v203==8 & v202==0 & v204==0 & v205==1| v203==7 & v202==2 & v204==0 & v205==0| v203==7 & v202==0 & v204==2 & v205==0| v203==7 & v202==0 & v204==0 & v205==2| v203==7 & v202==1 & v204==1 & v205==0| v203==7 & v202==0 & v204==1 & v205==1| v203==7 & v202==1 & v204==0 & v205==1| v203==6 & v202==3 & v204==0 & v205==0| v203==6 & v202==0 & v204==3 & v205==0| v203==6 & v202==0 & v204==0 & v205==3| v203==6 & v202==2 & v204==1 & v205==0| v203==6 & v202==0 & v204==2 & v205==1
| v203==6 & v202==1 & v204==0 & v205==2| v203==6 & v202==1 & v204==1 & v205==1| v203==5 & v202==4 & v204==0 & v205==0| v203==5 & v202==0 & v204==4 & v205==0| v203==5 & v202==0 & v204==0 & v205==4| v203==5 & v202==3 & v204==1 & v205==0| v203==5 & v202==0 & v204==3 & v205==1| v203==5 & v202==1 & v204==0 & v205==3| v203==5 & v202==2 & v204==1 & v205==1| v203==5 & v202==1 & v204==2 & v205==1| v203==5 & v202==1 & v204==1 & v205==2| v203==5 & v202==2 & v204==2 & v205==0| v203==5 & v202==0 & v204==2 & v205==2| v203==5 & v202==2 & v204==0 & v205==2| v203==4 & v202==3 & v204==1 & v205==1
| v203==4 & v202==1 & v204==3 & v205==1| v203==4 & v202==1 & v204==1 & v205==3| v203==4 & v202==1 & v204==2 & v205==2| v203==4 & v202==2 & v204==1 & v205==2| v203==4 & v202==2 & v204==2 & v205==1| v203==3 & v202==3 & v204==3 & v205==0| v203==3 & v202==0 & v204==3 & v205==3| v203==3 & v202==3 & v204==0 & v205==3| v204==9 & v202==0 & v203==0 & v205==0| v204==8 & v202==1 & v203==0 & v205==0| v204==8 & v202==0 & v203==1 & v205==0| v204==8 & v202==0 & v203==0 & v205==1| v204==7 & v202==2 & v203==0 & v205==0| v204==7 & v202==0 & v203==2 & v205==0| v204==7 & v202==0 & v203==0 & v205==2
| v204==7 & v202==1 & v203==1 & v205==0| v204==7 & v202==0 & v203==1 & v205==1| v204==7 & v202==1 & v203==0 & v205==1| v204==6 & v202==3 & v203==0 & v205==0| v204==6 & v202==0 & v203==3 & v205==0| v204==6 & v202==0 & v203==0 & v205==3| v204==6 & v202==2 & v203==1 & v205==0| v204==6 & v202==0 & v203==2 & v205==1| v204==6 & v202==1 & v203==0 & v205==2| v204==6 & v202==1 & v203==1 & v205==1| v204==5 & v202==4 & v203==0 & v205==0| v204==5 & v202==0 & v203==4 & v205==0| v204==5 & v202==0 & v203==0 & v205==4| v204==5 & v202==3 & v203==1 & v205==0| v204==5 & v202==0 & v203==3 & v205==1
| v204==5 & v202==1 & v203==0 & v205==3| v204==5 & v202==2 & v203==1 & v205==1| v204==5 & v202==1 & v203==2 & v205==1| v204==5 & v202==1 & v203==1 & v205==2| v204==5 & v202==2 & v203==2 & v205==0| v204==5 & v202==0 & v203==2 & v205==2| v204==5 & v202==2 & v203==0 & v205==2| v204==4 & v202==3 & v203==1 & v205==1| v204==4 & v202==1 & v203==3 & v205==1| v204==4 & v202==1 & v203==1 & v205==3| v204==4 & v202==1 & v203==2 & v205==2| v204==4 & v202==2 & v203==1 & v205==2| v204==4 & v202==2 & v203==2 & v205==1| v204==3 & v202==3 & v203==3 & v205==0| v204==3 & v202==0 & v203==3 & v205==3
| v204==3 & v202==3 & v203==0 & v205==3| v205==9 & v202==0 & v203==0 & v204==0| v205==8 & v202==1 & v203==0 & v204==0| v205==8 & v202==0 & v203==1 & v204==0| v205==8 & v202==0 & v203==0 & v204==1| v205==7 & v202==2 & v203==0 & v204==0| v205==7 & v202==0 & v203==2 & v204==0| v205==7 & v202==0 & v203==0 & v204==2| v205==7 & v202==1 & v203==1 & v204==0| v205==7 & v202==0 & v203==1 & v204==1| v205==7 & v202==1 & v203==0 & v204==1| v205==6 & v202==3 & v203==0 & v204==0| v205==6 & v202==0 & v203==3 & v204==0| v205==6 & v202==0 & v203==0 & v204==3| v205==6 & v202==2 & v203==1 & v204==0
| v205==6 & v202==0 & v203==2 & v204==1| v205==6 & v202==1 & v203==0 & v204==2| v205==6 & v202==1 & v203==1 & v204==1| v205==5 & v202==4 & v203==0 & v204==0| v205==5 & v202==0 & v203==4 & v204==0| v205==5 & v202==0 & v203==0 & v204==4| v205==5 & v202==3 & v203==1 & v204==0| v205==5 & v202==0 & v203==3 & v204==1| v205==5 & v202==1 & v203==0 & v204==3| v205==5 & v202==2 & v203==1 & v204==1| v205==5 & v202==1 & v203==2 & v204==1| v205==5 & v202==1 & v203==1 & v204==2| v205==5 & v202==2 & v203==2 & V204==0| v205==5 & v202==0 & v203==2 & v204==2| v205==5 & v202==2 & v203==0 & v204==2
| v205==4 & v202==3 & v203==1 & v204==1| v205==4 & v202==1 & v203==3 & v204==1| v205==4 & v202==1 & v203==1 & v204==3| v205==4 & v202==1 & v203==2 & v204==2| v205==4 & v202==2 & v203==1 & v204==2| v205==4 & v202==2 & v203==2 & v204==1| v205==3 & v202==3 & v203==3 & v204==0| v205==3 & v202==0 & v203==3 & v204==3| v205==3 & v202==3 & v203==0 & v204==3 ;
#delimit cr

Unfortunately I got a similar r(130) message again. Since I'm stuck with a 'too long' message at 9, and there are still 10 and 11 to consider, I'm not sure what to do at this point.

SN
Re: Too long command in Stata? [message #11636 is a reply to message #11635] Thu, 19 January 2017 13:12 Go to previous messageGo to next message
Bridgette-DHS is currently offline  Bridgette-DHS
Messages: 3190
Registered: February 2013
Senior Member
Following is another response from Senior DHS Stata Specialist, Tom Pullum:

Did you read the error statement? As it says, this "line" has too many characters and too many components. Beyond that, it is very ambiguous because you do not have parentheses. However, even if it were shorter, it would not execute. Can't you see that something like "if A & B | C & D" is ambiguous? Such a statement could be interpreted as "if (A & B) | (C & D)" or as "if A & (B | C) & D" or a few other variations. What is it that you want to do? You certainly do not need to run through all possible combinations of v202, v203, v204, and v205, which is what you seem to be doing. If you will insert left and right parentheses to remove the ambiguity, I will then suggest how you could reduce the length of the line.


Re: Too long command in Stata? [message #11638 is a reply to message #11636] Fri, 20 January 2017 04:11 Go to previous messageGo to next message
nwegbus is currently offline  nwegbus
Messages: 15
Registered: December 2015
Member
Thank you Tom. I knew I must have been doing something (or possibly many things) wrong.

So what I'm trying to do is figure out the total number of children each woman has. DHS (Nigeria) 2013 does not have this as a variable in the dataset, so what I decided to do was to combine 4 different variables (v202, v203, v204 and v205, i.e., sons at home, daughters at home, sons elsewhere and daughters elsewhere) to get total number of children per woman.

With regard to applying parentheses, (using just one line as an example) what it would look like is this:

replace achild=9 if (v202==9 & v203==0 & v204==0 & v205==0)| (v202==8 & v203==1 & v204==0 & v205==0)|

Thank you once again for your help and continued patience with me. It is much appreciated.

Som
Re: Too long command in Stata? [message #11663 is a reply to message #11638] Mon, 23 January 2017 06:23 Go to previous messageGo to next message
Bridgette-DHS is currently offline  Bridgette-DHS
Messages: 3190
Registered: February 2013
Senior Member
Following is a response from Senior DHS Stata Specialist, Tom Pullum:


Apparently you do not know that arithmetic can be done within a recode statement. The number of children ever born is included in the data as v201. It is the sum of v202 through v207. If you want the number of LIVING children, you can calculate that in two ways, either with "gen nlch=v202+v203+v204+v205" or with "gen nlch=v201-(v206+v207)". Much simpler than what you were doing. Fortunately, this group of variables (v201 through v209) never has codes for "not applicable" or "don't know" or "missing", etc. If the component variables for a recode DO have such codes, you have to be sure to take them into account.
Re: Too long command in Stata? [message #11667 is a reply to message #11663] Mon, 23 January 2017 12:03 Go to previous message
nwegbus is currently offline  nwegbus
Messages: 15
Registered: December 2015
Member
Thank you. You are correct. I did not know this. I'm learning Stata, as I go along, by actually doing real life stuff with it. True, I've had a lot of Stata classes, but I'm one of those people that learn best when I can apply abstract stuff to real life things that I actually care about.

Once again, thank you for all you do :)
SN
Previous Topic: stset for survival analysis for complex survey in IR files using Stata
Next Topic: Using community-level variables in regression models
Goto Forum:
  


Current Time: Sat Nov 9 09:08:59 Coordinated Universal Time 2024