Discussion:
Stata to SPSS
(too old to reply)
Amit Kumar
2021-02-21 06:27:13 UTC
Permalink
Hello All,

How can I run the same command in SPSS?
bysort hhid: egen hhincome=total(totalinc)

where hhid is my key variable.

Thanks in advance.
Amit
Rich Ulrich
2021-02-22 04:04:56 UTC
Permalink
On Sat, 20 Feb 2021 22:27:13 -0800 (PST), Amit Kumar
<***@cbgaindia.org> wrote:

SUBJECT: Stata to SPSS ( should have been in the text, too )
Post by Amit Kumar
Hello All,
How can I run the same command in SPSS?
bysort hhid: egen hhincome=total(totalinc)
where hhid is my key variable.
If Bruce is not on vacation, he might pop in with an answer,
since he uses Stata.

Else, you /could/ describe what the command does ...
<Google> it looks like bysort does what Split Files does.

If you just want to get totals for categories of hhid,
you would Aggregate on hhid, getting SUM(totalinc).

And
[cribbed from an older message by Bruce]

The SPSS mailing list (http://spssx-discussion.1045642.n5.nabble.com/)
is a lot more active than this forum these days. You might want to
join that list (if not already a member) and post your question there.
Via the page given above, click on -more options- near the top for
info on how to subscribe. HTH.
--
Rich Ulrich
Bruce Weaver
2021-06-25 15:28:52 UTC
Permalink
Post by Rich Ulrich
On Sat, 20 Feb 2021 22:27:13 -0800 (PST), Amit Kumar
SUBJECT: Stata to SPSS ( should have been in the text, too )
Post by Amit Kumar
Hello All,
How can I run the same command in SPSS?
bysort hhid: egen hhincome=total(totalinc)
where hhid is my key variable.
If Bruce is not on vacation, he might pop in with an answer,
since he uses Stata.
Else, you /could/ describe what the command does ...
<Google> it looks like bysort does what Split Files does.
If you just want to get totals for categories of hhid,
you would Aggregate on hhid, getting SUM(totalinc).
And
[cribbed from an older message by Bruce]
The SPSS mailing list (http://spssx-discussion.1045642.n5.nabble.com/)
is a lot more active than this forum these days. You might want to
join that list (if not already a member) and post your question there.
Via the page given above, click on -more options- near the top for
info on how to subscribe. HTH.
--
Rich Ulrich
I did not see this until now, several months later. (My ISP stopped providing news server support, and the "new" and not so improved Google Groups does not notify me when new posts appear.) So this is no doubt too late for the OP, but I'll post it for the archives. The following AGGREGATE command in SPSS will give the same result as the OP's line of Stata code:

AGGREGATE
/OUTFILE=* MODE=ADDVARIABLES
/BREAK=hhid
/hhincome=SUM(totalinc).

Here is a demonstration, starting with Stata, and following with SPSS.

// Start of Stata code
// Use data file from this UCLA tutorial to illustrate:
// https://stats.idre.ucla.edu/stata/modules/reshaping-data-wide-to-long/

use https://stats.idre.ucla.edu/stat/stata/modules/faminc, clear
reshape long faminc, i(famid) j(year)
list, clean noobs
// Rename variables to match OPs variable names
rename (famid faminc) (hhid totalinc)
list, clean noobs
// Now execute the OP's Stata code and list the results
bysort hhid: egen hhincome=total(totalinc)
list, clean noobs
// End of Stata code

*** Start of SPSS syntax ***.
DATA LIST LIST / hhid year totalinc (3F8.0).
BEGIN DATA
1 96 40000
1 97 40500
1 98 41000
2 96 45000
2 97 45400
2 98 45800
3 96 75000
3 97 76000
3 98 77000
END DATA.

AGGREGATE
/OUTFILE=* MODE=ADDVARIABLES
/BREAK=hhid
/hhincome=SUM(totalinc).

LIST.
*** End of SPSS syntax ***.

Output from the final list command in the Stata code:

. list, clean noobs

hhid year totalinc hhincome
1 96 40000 121500
1 97 40500 121500
1 98 41000 121500
2 96 45000 136200
2 97 45400 136200
2 98 45800 136200
3 96 75000 228000
3 97 76000 228000
3 98 77000 228000

Output from the LIST command in the SPSS code:

hhid year totalinc hhincome

1 96 40000 121500.0
1 97 40500 121500.0
1 98 41000 121500.0
2 96 45000 136200.0
2 97 45400 136200.0
2 98 45800 136200.0
3 96 75000 228000.0
3 97 76000 228000.0
3 98 77000 228000.0
Amit Kumar
2021-06-25 18:30:02 UTC
Permalink
Post by Bruce Weaver
Post by Rich Ulrich
On Sat, 20 Feb 2021 22:27:13 -0800 (PST), Amit Kumar
SUBJECT: Stata to SPSS ( should have been in the text, too )
Post by Amit Kumar
Hello All,
How can I run the same command in SPSS?
bysort hhid: egen hhincome=total(totalinc)
where hhid is my key variable.
If Bruce is not on vacation, he might pop in with an answer,
since he uses Stata.
Else, you /could/ describe what the command does ...
<Google> it looks like bysort does what Split Files does.
If you just want to get totals for categories of hhid,
you would Aggregate on hhid, getting SUM(totalinc).
And
[cribbed from an older message by Bruce]
The SPSS mailing list (http://spssx-discussion.1045642.n5.nabble.com/)
is a lot more active than this forum these days. You might want to
join that list (if not already a member) and post your question there.
Via the page given above, click on -more options- near the top for
info on how to subscribe. HTH.
--
Rich Ulrich
AGGREGATE
/OUTFILE=* MODE=ADDVARIABLES
/BREAK=hhid
/hhincome=SUM(totalinc).
Here is a demonstration, starting with Stata, and following with SPSS.
// Start of Stata code
// https://stats.idre.ucla.edu/stata/modules/reshaping-data-wide-to-long/
use https://stats.idre.ucla.edu/stat/stata/modules/faminc, clear
reshape long faminc, i(famid) j(year)
list, clean noobs
// Rename variables to match OPs variable names
rename (famid faminc) (hhid totalinc)
list, clean noobs
// Now execute the OP's Stata code and list the results
bysort hhid: egen hhincome=total(totalinc)
list, clean noobs
// End of Stata code
*** Start of SPSS syntax ***.
DATA LIST LIST / hhid year totalinc (3F8.0).
BEGIN DATA
1 96 40000
1 97 40500
1 98 41000
2 96 45000
2 97 45400
2 98 45800
3 96 75000
3 97 76000
3 98 77000
END DATA.
AGGREGATE
/OUTFILE=* MODE=ADDVARIABLES
/BREAK=hhid
/hhincome=SUM(totalinc).
LIST.
*** End of SPSS syntax ***.
. list, clean noobs
hhid year totalinc hhincome
1 96 40000 121500
1 97 40500 121500
1 98 41000 121500
2 96 45000 136200
2 97 45400 136200
2 98 45800 136200
3 96 75000 228000
3 97 76000 228000
3 98 77000 228000
hhid year totalinc hhincome
1 96 40000 121500.0
1 97 40500 121500.0
1 98 41000 121500.0
2 96 45000 136200.0
2 97 45400 136200.0
2 98 45800 136200.0
3 96 75000 228000.0
3 97 76000 228000.0
3 98 77000 228000.0
Thanks Bruce for your response, Although I solved the issue in a different but long way.
This will be useful in future if I face the same issue.

Continue reading on narkive:
Loading...