非连续高度变量的变换
Transformation of Non-Continuous Height Variable
我无法有效地转换样本中编码错误的高度变量。
变量编码如下:
Height
ID1 601
ID1 601
ID1 601
ID3 409
ID3 410
ID4 511
. .
. .
. .
ID100 400
如您所见,变量同时编码为英尺和英寸,其中 601
等于 6 英尺 1 英寸,511
等于 5 英尺 11 英寸等。
我的目标是将这些数字转换成英寸:
replace Height = 48 if Height == 400
replace Height = 49 if Height == 401
replace Height = 50 if Height == 402
replace Height = 51 if Height == 403
.
.
.
replace Height = 83 if Height == 611
如何使用循环高效地编写代码?
下面的代码假定 Height
的每个观察值恰好包含三个数字:
tostring Height, generate(Height_string)
/* Generate a new variable which is a string-version of Height
(so that we can get the individual digits) */
generate feet = substr(Height_string, 1, 1)
/* From the first character in the string, select one character */
generate inch = substr(Height_string, 2, 2)
/* From the second character in the string, select two characters.
An equivalent alternative would have been
generate inch = substr(Height_string, -2, 2)
which from the second to last character selects two characters */
destring feet inch, replace
/* Convert these two new variables to numeric */
generate tot_inch = feet * 12 + inch
/* Generate a new variable which measure only in inches. */
这可以作为纯数字运算在一行中完成。转换为字符串并再次转换回来是完全没有必要的。不需要循环。
generate wanted = 12 * floor(height/100) + mod(height, 100)
我无法有效地转换样本中编码错误的高度变量。
变量编码如下:
Height
ID1 601
ID1 601
ID1 601
ID3 409
ID3 410
ID4 511
. .
. .
. .
ID100 400
如您所见,变量同时编码为英尺和英寸,其中 601
等于 6 英尺 1 英寸,511
等于 5 英尺 11 英寸等。
我的目标是将这些数字转换成英寸:
replace Height = 48 if Height == 400
replace Height = 49 if Height == 401
replace Height = 50 if Height == 402
replace Height = 51 if Height == 403
.
.
.
replace Height = 83 if Height == 611
如何使用循环高效地编写代码?
下面的代码假定 Height
的每个观察值恰好包含三个数字:
tostring Height, generate(Height_string)
/* Generate a new variable which is a string-version of Height
(so that we can get the individual digits) */
generate feet = substr(Height_string, 1, 1)
/* From the first character in the string, select one character */
generate inch = substr(Height_string, 2, 2)
/* From the second character in the string, select two characters.
An equivalent alternative would have been
generate inch = substr(Height_string, -2, 2)
which from the second to last character selects two characters */
destring feet inch, replace
/* Convert these two new variables to numeric */
generate tot_inch = feet * 12 + inch
/* Generate a new variable which measure only in inches. */
这可以作为纯数字运算在一行中完成。转换为字符串并再次转换回来是完全没有必要的。不需要循环。
generate wanted = 12 * floor(height/100) + mod(height, 100)