Hello everyone,
I am working with the following data with variables HHID (household ID) and some variables s1, s2, s3...s147
I want to write a loop where instead of missing values for s3 (as in line 1 of the data), I want to replace s4 value to be s3, and the next available value for example s45 to be s4. I wrote the following code:
Naturally it didn't work. Is there anyway to fix this code?
I am working with the following data with variables HHID (household ID) and some variables s1, s2, s3...s147
I want to write a loop where instead of missing values for s3 (as in line 1 of the data), I want to replace s4 value to be s3, and the next available value for example s45 to be s4. I wrote the following code:
Code:
forval i = 1/144{
replace s`i' = s`i+1' if s`i' ==.
}
Code:
* Example generated by -dataex-. For more info, type help dataex clear input long HHID int(s1 s2 s3 s4 s5 s6) 39846680 1377 554 . 407 . . 39849080 1122 25 . . . . 39849597 1858 1333 . . 1075 586 39860292 1717 1711 1712 1713 473 268 39860383 1789 869 . 807 874 . 39864269 1750 1746 860 800 800 860 39865883 177 395 251 . 41 42 39868537 194 968 967 1336 . 1335 39870715 1704 683 697 1560 1559 . 39871735 744 1607 . . . 733 39953078 . . . 1128 . . 40102812 488 142 . 142 . 1309 40104157 1723 1729 1068 1943 . 1611 40104161 1739 1743 1743 1742 1735 1449 40104320 1870 1871 1521 1932 . 1933 39884541 1457 1456 1282 . . 1281 39884589 1704 684 704 722 . . 39885252 1744 1753 1752 1747 812 . 39885589 1704 683 696 . 1943 . 39885717 . . 1501 . 1500 1463 39885987 1370 . 620 486 190 . 39886070 1414 716 481 713 775 776 39886077 757 1282 . . 1281 1310 39886620 1734 1733 585 . . . 39886640 1291 1290 1294 . 1293 1292 39886694 1684 485 1309 . 142 . 39887177 1870 1871 1521 1932 . 1933 39887300 1704 682 719 677 618 473 39887620 1367 1860 1735 1449 426 1681 39888105 . 1899 1568 1569 1570 1571 39888238 1701 1082 473 268 511 . 39888754 1784 1879 1880 1881 1882 804 39889051 628 455 585 1733 1734 . 39889269 . . . . . . 39889499 1062 1091 . 1465 . . 39889668 1717 1711 1712 1713 473 268 39890325 1784 972 935 903 1591 900 39890706 1897 1898 695 1851 1894 1893 39891038 744 1607 . . . 733 39891136 . 1086 728 1068 1085 1718 39891607 1087 . 1086 728 681 1698 39891622 1370 . 620 486 190 . 39891797 1717 1711 1725 1082 473 472 39891936 1870 1871 1521 1932 . 1933 39892365 1719 1713 473 472 1823 1824 39892651 1370 . 620 486 190 166 39893968 1099 1098 . 454 1437 1438 39894099 . . 1448 1014 . . 39894781 1676 1746 1750 1741 1749 804 39894802 190 486 620 1370 . . 39895298 620 486 190 166 578 . 39895432 1415 . . . . . 39895783 763 1399 493 492 75 407 39896001 1087 . 1086 728 681 1698 39896197 1785 1609 1610 1611 1943 . 39896267 1704 683 696 . 1943 . 39896504 1702 1728 . 1729 1068 1943 39896661 1717 1711 1712 17 677 719 39896722 1718 1085 1068 1943 . 1611 39897204 . 585 1733 1734 447 1231 39897215 166 578 . 593 192 . 39897737 1865 1893 1894 . . . 39898739 1558 . . . . . 39898868 1870 1877 . . . . 39899564 1370 . 620 486 190 . 39899894 1893 1865 1858 1333 . . 39900010 1723 1729 1068 1943 . 1611 39900272 1704 683 696 . 1943 . 39900281 1723 1729 1708 1691 . 1690 39900531 758 1321 1343 1766 . 1883 39901062 1283 1276 680 1708 1709 1710 39901104 188 . 1682 . 1681 426 39901674 304 337 . 794 793 283 39902297 . 711 679 . 1708 1709 39902426 1704 683 696 . 1943 . 39902451 1704 682 719 713 481 717 39902515 . 1086 728 681 1698 1998 39902529 1704 682 719 677 618 473 39902574 1360 . 1359 . . 1933 39902642 . 585 1733 1734 447 1231 39902861 165 1324 1323 1322 1320 1319 39903075 1717 1718 1719 1713 473 268 39903175 1719 1713 473 268 511 . 39903403 620 486 190 166 578 . 39904048 1717 1711 1725 1082 473 268 39904521 . . . 711 679 . 39905609 1704 683 696 . 1943 . 39906047 1701 1082 473 268 511 . 39906539 752 609 1039 . 1040 . 39906566 620 486 190 166 578 . 39906747 1784 972 935 935 972 1784 39907007 1558 . . . . . 39907257 . 1899 1568 1569 1570 1571 39907855 1715 775 776 682 719 677 39908362 . 1894 1893 1865 . . 39908696 1717 1711 1712 1713 473 268 39908873 1704 682 719 677 618 473 39908979 . 1086 728 681 1708 1709 39910067 1865 1893 1894 1851 . 695 39910390 1723 1729 1068 1943 . 1611 end

Comment