根据字符串包含的内容更改变量值

时间:2018-10-08 19:25:53

标签: stata

假设您有几个变量:

+--------------------------+------------+------------+-----------+-------+
|                          | Population | Median_Age | Sex_Ratio | GDP   |
| Country                  |            |            |           |       |
+--------------------------+------------+------------+-----------+-------+
| United States of America | 3999       |            | 1.01      | 16000 |
+--------------------------+------------+------------+-----------+-------+
| Afghanistan              | 544        | 19         | 0.97      | 4456  |
+--------------------------+------------+------------+-----------+-------+
| China                    | 5000       | 26         | 0.96      | 10000 |
+--------------------------+------------+------------+-----------+-------+

让我们假设Median_Age下的United States of America为空。

如果Country包含美利坚合众国或美利坚合众国,我如何将此缺失值替换为27?

1 个答案:

答案 0 :(得分:3)

这是一个经过修改的示例,可以更好地说明解决方案:

clear

input strL Country Population Median_Age Sex_Ratio GDP 
"United States of America" 3999 . 1.01 5000
"Afghanistan" 544 19 0.97 457
"United Emirates" 7546 44 7.01 2000
"China" 10000 26 0.96 3400
"United Fictionary Nation" 6789 . 8.03 7689
end

list, abbreviate(10)

     +-----------------------------------------------------------------------+
     |                  Country   Population   Median_Age   Sex_Ratio    GDP |
     |-----------------------------------------------------------------------|
  1. | United States of America         3999            .        1.01   5000 |
  2. |              Afghanistan          544           19         .97    457 |
  3. |          United Emirates         7546           44        7.01   2000 |
  4. |                    China        10000           26         .96   3400 |
  5. | United Fictionary Nation         6789            .        8.03   7689 |
     +-----------------------------------------------------------------------+

replace Median_Age = 27 if ( strmatch(Country, "*United States*") | ///
                             strmatch(Country, "*United*") ) & ///
                             missing(Median_Age)

list, abbreviate(10)

     +-----------------------------------------------------------------------+
     |                  Country   Population   Median_Age   Sex_Ratio    GDP |
     |-----------------------------------------------------------------------|
  1. | United States of America         3999           27        1.01   5000 |
  2. |              Afghanistan          544           19         .97    457 |
  3. |          United Emirates         7546           44        7.01   2000 |
  4. |                    China        10000           26         .96   3400 |
  5. | United Fictionary Nation         6789           27        8.03   7689 |
     +-----------------------------------------------------------------------+