【问题标题】:Generating observations for non existing observations为不存在的观察生成观察
【发布时间】:2016-07-14 08:21:21
【问题描述】:

我目前在 SAS 中遇到问题。我正在写关于职业伤害和此类事故造成的工资损失的硕士论文。我拥有一个数据集,其中包含给定人口的月工资信息。该数据集包含每个人的特定标识符以及一系列背景信息,如教育、性别等。如果个人收到了特定月份的工资支付,他或她就在我的数据集中,该特定月份具有上述-提到的信息。如果此人未收到特定月份的工资支付(例如,由于失业、假期、疾病等),则该人在该特定月份的数据集中找不到 - 即使此人可能已收到工资支付前一个月和后一个月。因此,例如,一个工作了一整年的人在数据集中有 12 个观察值。如果某人由于某种原因没有工作 2 个月,则该人只有 10 次观察。

如果我不处理这个问题,我的估计很可能会出现正偏差。一个人可能在给定月份遭受工伤,然后在下个月返回,该人因病缺勤的月份应构成工资支付为零,但仍包含“背景”信息。

我已经上传了一个数据集的例子,可以在这里找到:

https://www.dropbox.com/s/0zkr0430menotdf/Data.xlsx?dl=0

数据包含三个人 (1,2,3) 的工资信息、教育组、性别。标识符为 2 的人在第 8 个月没有收到付款,因此该月没有任何观察结果。

我不知道如何制作一个 SAS 程序来为那个人填写信息。我想对第 8 个月的人 2 进行新的观察,工资支付为零,但上个月其他变量的信息滞后。在我的原始数据集中,我有数千人,其中一些潜力可能有多个不存在的工资信息。

【问题讨论】:

    标签: sas missing-data


    【解决方案1】:

    最简单的方法是构建一个master 表,如果您的数据完美,该表将包含月份和个人的每一个组合。一旦你有了这个数据集,你就离开加入你的错误真实数据以获得完整的视图,而不管丢失的数据。

    不需要滞后信息,因为无论如何所有列都会存在。

    这是一个虚拟示例(我称用户而不是个人)

    /* table with 1-12 that represents months ids*/
    data months(drop=i);
    do i=1 to 12;
     month=i;
     output;
    end;
    run;
    /* table with unique user ids*/
        data users;
    user_id = 1000;gender='M';output;
    user_id = 1001;gender='F';output;
    run;
    
    /* Your data*/
    data my_data;
    input user_id month salary;
    cards;
    1001 1 1500
    1001 2 1500
    1001 3 1500
    1001 4 1500
    1001 5 1500
    1001 6 1500
    1001 7 1500
    1001 8 1500
    1001 9 1500
    1001 10 1500
    1001 11 1500
    1001 12 1500
    1000 1 800
    1000 2 800
    1000 3 800
    1000 4 800
    ;
    run;
    
    /* Step1: Build a full combination of (cartesian join) of months & users
      In this case is 12 months x 2 users = 24 records
    */
    proc sql;
    create table master_tbl as
        select * from months, users;
    quit;
    
    /* Step2: Left join your 'faulty' data against the master table to get a full view
    for each user */
    
    proc sql;
    create table full_view as
    Select t1.*, t2.salary from master_tbl t1 left join my_data t2
                                on t1.user_id = t2.user_id and
                                   t1.month = t2.month;
    quit;
    

    full_view 数据集将包含非缺失和缺失案例。当工资信息丢失时,您可以检测到丢失的案例。

    注意:如果您认为您的 wage 变量也有问题,请在您的数据中创建一个虚拟标志 (f=1) 并将其带入 full_view 数据集以突出显示缺失的数据。

    编辑 1: 对于替换数据,您可以这样做:

    data full_view;
     set full_view;
     if salary = . then do;
       salary = 0;
       salary_ind = (salary = 0); /*Dummy to keep track of what you have imputed*/
    end;
    run;
    

    希望对你有帮助

    【讨论】:

    • 刚刚添加了额外的位用于估算缺失值
    • 还将性别添加到用户表中
    • 非常感谢,奥尔顿!我会试试的!我没想过要那样做。
    • 没问题 - 如果您对我的回答感到满意,请接受它
    【解决方案2】:

    与 Altons 的解决方案非常相似,不同之处在于我在 PROC SQL 步骤中替换了工资的缺失值。

    /*Load the data*/
    PROC SQL;
        CREATE TABLE have AS 
            SELECT t1.Identifier, 
                t1.Month, 
                t1.Wage, 
                t1.'education category'n, 
                t1.gender
            FROM WORK.DATA t1
                ORDER BY identifier, month;
    QUIT;
    
    
    /*Create a dataset with 12 observations of each id.*/
    data a;
        do i=1 to 3;
            do j=1 to 12;
                identifier=i;
                month=j;
                output;
            end;
        end;
    
        drop i j;
    run;
    
    
    /*Merge the dataset above with the original dataset, replacing missing values of wage with 0.*/
    proc sql;
        create table ab as
            select a.*, coalesce(b.wage,0) as wage, b.'education category'n, b.gender
                from a
                    left join have as b on a.identifier=b.identifier and a.month=b.month;
    quit;
    
    
    /*Use the update statement to carry forward previous non-missing values.*/
    data want;
        update a (obs=0) ab;
        by identifier;
        output;
    run;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-12-14
      • 2014-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-09
      • 1970-01-01
      相关资源
      最近更新 更多