如何将矩阵从一个数据集保存到另一个数据集?

时间:2015-08-18 08:25:25

标签: matrix stata

我已经从A创建了一个矩阵dataset1,我希望稍后在dataset2中使用它。

如何以编程方式保存此矩阵并将其导入dataset2

1 个答案:

答案 0 :(得分:2)

请考虑以下玩具数据集:

    a:visited {
        color: red;
    }

可以使用/* create dataset 1 */ clear set obs 5 forvalues i = 1 / 5 { generate norm`i' = rnormal(10, 20) } list +----------------------------------------------------------+ | norm1 norm2 norm3 norm4 norm5 | |----------------------------------------------------------| 1. | 29.184 47.57735 -6.06845 47.43953 12.10697 | 2. | 9.9639 65.09492 31.92023 18.47133 39.01292 | 3. | 20.88154 -2.251937 1.185946 22.67908 -11.98451 | 4. | 10.03257 13.94616 -10.22853 18.34467 37.34412 | 5. | 17.15362 42.20448 30.38455 -.5586708 20.34926 | +----------------------------------------------------------+ save data1, replace /* create dataset 2 */ clear set obs 5 forvalues i = 1 / 5 { generate unif`i' = runiform() } list +------------------------------------------------------+ | unif1 unif2 unif3 unif4 unif5 | |------------------------------------------------------| 1. | .4398566 .222692 .359981 .8840723 .840627 | 2. | .8955406 .7279246 .7385288 .1269085 .2610574 | 3. | .6760237 .5028067 .9236897 .2413106 .8938763 | 4. | .9666038 .0491344 .0098985 .4427792 .8565752 | 5. | .4118744 .368421 .1528643 .8636661 .0944128 | +------------------------------------------------------+ save data2, replace 命令来做到这一点:

svmat

请注意,如果尚未调用use data1, clear mkmat norm*, matrix(A) use data2, clear matrix list A A[5,5] norm1 norm2 norm3 norm4 norm5 r1 29.184 47.577354 -6.0684505 47.439529 12.106971 r2 9.9638996 65.094917 31.920233 18.471329 39.01292 r3 20.88154 -2.2519367 1.1859455 22.679077 -11.984506 r4 10.032575 13.946158 -10.228531 18.344669 37.344124 r5 17.153618 42.204475 30.384546 -.55867082 20.349257 svmat A, names(norm) list +-----------------------------------------------------------------------------------------------------------------+ | unif1 unif2 unif3 unif4 unif5 norm1 norm2 norm3 norm4 norm5 | |-----------------------------------------------------------------------------------------------------------------| 1. | .4398566 .222692 .359981 .8840723 .840627 29.184 47.57735 -6.06845 47.43953 12.10697 | 2. | .8955406 .7279246 .7385288 .1269085 .2610574 9.9639 65.09492 31.92023 18.47133 39.01292 | 3. | .6760237 .5028067 .9236897 .2413106 .8938763 20.88154 -2.251937 1.185946 22.67908 -11.98451 | 4. | .9666038 .0491344 .0098985 .4427792 .8565752 10.03257 13.94616 -10.22853 18.34467 37.34412 | 5. | .4118744 .368421 .1528643 .8636661 .0944128 17.15362 42.20448 30.38455 -.5586708 20.34926 | +-----------------------------------------------------------------------------------------------------------------+ 和/或clear matrix,则此解决方案将起作用。