Warm tip: This article is reproduced from serverfault.com, please click
sas

其他-使用SAS的proc sql或data步骤替换表中单个单元格的值?

(其他 - Using SAS' proc sql or data step to replace the value of a single cell in a table?)

发布于 2020-12-03 10:41:03

也许我只是使用了错误的搜索词:我有一张大桌子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. ---;

如何更新列z的那些情况下x=42,并y=21&new_value

如果数据步骤速度过快,我也很满意。我只想修改表,而不要创建一个新表,因为表确实很大。

参考

Questioner
B--rian
Viewed
11
crow16384 2020-12-03 19:00:29

让我们为执行练习做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;

此处仅以键名称,性别和年龄为例。