【问题标题】:Using SAS' proc sql or data step to replace the value of a single cell in a table?使用 SAS 的 proc sql 或数据步骤替换表中单个单元格的值?
【发布时间】:2020-12-03 19:00:29
【问题描述】:

可能我只是使用了错误的搜索词:我有一个大表 have 有多个列(例如 xyz)和不同的行,我打算替换一个值包含我保存在名为new_value 的宏变量中的值。

读取相应单元格的当前值old_value 很简单:

%let new_value = 4711;
proc sql noprint;
   select z into: old_value
   from have
   where x = 42 and y = 21;
quit;
%put --- f(42,21) = &old_value. ---;

对于 x=42y=21&new_value 的情况,如何更新列 z

如果数据步骤相当快,我也会对它感到满意。我只想修改表,而不是创建新表,因为表真的很大。

参考文献

【问题讨论】:

    标签: sas


    【解决方案1】:

    让我们临时复制 SASHELP.CLASS 数据以进行练习。

    data class;
      set sashelp.class;
    run;
    

    选择一个字符串值

    %let new_value=90;
    proc sql noprint;
       select weight into :old_value from class where name='Thomas' and Age=11;
    quit;
    %put --- f(Tomas,11) = &old_value. ---;
    

    SQL 中的更新很简单,你有条件:

    proc sql;
      update class set weight=&new_value where name='Thomas' and Age=11;
    quit;
    

    PROC SQL 可用于通过 VIEW 更新多个表,但这是另一个问题。

    在数据步骤中您可以使用事务(但需要数据排序,这不是免费的)。让我们使用 CLASS 数据集的初始副本:

    proc sort data=class;
      by name sex age;
    run;
    

    准备示例交易数据(可能多于一条记录):

    data transaction;
      set class;
      where name='Thomas';
      weight=&new_value;
    run;
    
    proc sort data=transaction;
      by name sex age;
    run;
    

    并进行更新:

    data class;
      update class transaction;
      by name sex age;
    run;
    

    此处的键名、性别和年龄只是示例。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-10
      • 1970-01-01
      • 1970-01-01
      • 2019-05-16
      • 2023-03-18
      • 1970-01-01
      相关资源
      最近更新 更多