Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Stata to SPSS

103 views
Skip to first unread message

Amit Kumar

unread,
Feb 21, 2021, 1:27:15 AM2/21/21
to
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

unread,
Feb 21, 2021, 11:05:04 PM2/21/21
to
On Sat, 20 Feb 2021 22:27:13 -0800 (PST), Amit Kumar
<am...@cbgaindia.org> wrote:

SUBJECT: Stata to SPSS ( should have been in the text, too )
>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

unread,
Jun 25, 2021, 11:28:54 AM6/25/21
to
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

unread,
Jun 25, 2021, 2:30:05 PM6/25/21
to
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.
0 new messages