I have a dataset like this
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input float(newdate dateid) long userid double amount 21297 90 1 1 21299 92 1 2 21303 96 1 3 21304 97 1 1 21305 98 1 4 21306 99 2 1 21306 99 1 9 21307 100 1 1 end format %td newdate
Code:
* Reshape to WIDE in order to replace missing values to 0
reshape wide amount, i(newdate dateid) j(userid)
replace amount1 = 0 if amount1 == .
replace amount2 = 0 if amount2 == .
list
* Reshape back to LONG
reshape long
list, sepby(newdate)
+--------------------------------------+
| newdate dateid userid amount |
|--------------------------------------|
1. | 23apr2018 90 1 1 |
2. | 23apr2018 90 2 0 |
|--------------------------------------|
3. | 25apr2018 92 1 2 |
4. | 25apr2018 92 2 0 |
|--------------------------------------|
5. | 29apr2018 96 1 3 |
6. | 29apr2018 96 2 0 |
|--------------------------------------|
7. | 30apr2018 97 1 1 |
8. | 30apr2018 97 2 0 |
|--------------------------------------|
9. | 01may2018 98 1 4 |
10. | 01may2018 98 2 0 |
|--------------------------------------|
11. | 02may2018 99 1 9 |
12. | 02may2018 99 2 1 |
|--------------------------------------|
13. | 03may2018 100 1 1 |
14. | 03may2018 100 2 0 |
+--------------------------------------+I know in Stata we can do it with loop but I can not manage to do it myself. Thank you for your help.
Best regards,
Thong Nguyen
0 Response to Please help me to create a loop to replace missing values to 0
Post a Comment