I have a panel dataset. On the first row and first columns are variables which are names of 100 countries. This table is a bilateral dataset of the investment between countries.
I set all names in the first row as variables. I want to reshape this dataset, from wide to long.
Most variables are numeric but some are string because the string variables sometimes have C in values.
I want to replace all these "C" values with 0. Then I want to make all string variables become numeric as the others.
I guess I must use "loop". What can I do?
My table looks like below (not exactly but same). The variables Argentina and Australia are string because they contain "C".
My target in the end is to reshape this table from wide to long, so I suppose to rename the variables as follows:
and then run the following code:
to get the table as below:
So in order to get my target, I must have variables in the same type first. That's why I ask the question above.
Many thanks!
I set all names in the first row as variables. I want to reshape this dataset, from wide to long.
Most variables are numeric but some are string because the string variables sometimes have C in values.
I want to replace all these "C" values with 0. Then I want to make all string variables become numeric as the others.
I guess I must use "loop". What can I do?
My table looks like below (not exactly but same). The variables Argentina and Australia are string because they contain "C".
Invested_country | Albania | Argentina | Australia | Austria |
Albania | 142341 | C | C | 29398 |
Argentina | 244323 | 912824 | 195335 | 89324 |
Australia | 857343 | 123122 | C | 15235 |
Austria | 423479 | 123123 | 209473 | 12345 |
Invested_country | InvestorAlbania | InvestorArgentina | InvestorAustralia | InvestorAustria |
Albania | ||||
Argentina | ||||
Australia | ||||
Austria |
HTML Code:
generate id=_n reshape long Investor, i(id) j(ivstor,string)
Invested_country | ivstor | Investor |
Albania | Albania | 142341 |
Argentina | Albania | 244323 |
Australia | Albania | 857343 |
Austria | Albania | 423479 |
Albania | Argentina | 0 |
Argentina | Argentina | 912824 |
Australia | Argentina | 123122 |
Austria | Argentina | 123123 |
Albania | Australia | 0 |
Argentina | Australia | 195335 |
Australia | Australia | 0 |
Austria | Australia | 209473 |
Albania | Austria | 29398 |
Argentina | Austria | 89324 |
Australia | Austria | 15235 |
Austria | Austria | 12345 |
... | ... | ... |
Many thanks!
Comment