Change rows to columns based on key ID's

允我心安 提交于 2019-12-24 07:58:26

问题


So here's an example of what I have, and an explanation of what I am looking for. I can't seem to get the restructure function in SPSS to work properly, could be the wrong tool for the job. Any help would be appreciated. Thank you!

  • ID | Car_Make | Car_Model
  • 999, Subaru, WRX
  • 867, Volvo, 240
  • 999,Acura, TSX

  • ID | Car_Make1 | Car_Model1 | Car_Make2 | Car_Model2.....(Dependent on list)
  • 999, Subaru, WRX, Acura, TSX
  • 867, Volvo, 240

Any thoughts? Thank you!


回答1:


What you are missing is a key\index variable. Try this:

* first recreating your sample data.
data list list/ ID (f10) Car_Make  Car_Model (2a20).
begin data
999, "Subaru", "WRX"
867, "Volvo", "240"
999,"Acura", "TSX"
end data.

* now creating an index and restructuring.
sort cases by ID.
compute ind=1.
format ind(f2).
if $casenum>1 and ID=lag(ID) ind=lag(ind)+1.
casestovars /id=ID/index=ind/sep="_".


来源:https://stackoverflow.com/questions/52204173/change-rows-to-columns-based-on-key-ids

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!