简体   繁体   English

使用 SAS 的 proc sql 或数据步骤来替换表中单个单元格的值?

[英]Using SAS' proc sql or data step to replace the value of a single cell in a table?

Probably I am simply using the wrong search terms: I have a large table have with multiple columns (eg x , y , z ) and various rows, and I intend to replace a single value within with a value I have saved in a macro variable called new_value .可能我只是使用了错误的搜索词:我have一个包含多列(例如xyz )和多行的大表,我打算用我保存在宏变量中的值替换其中的单个值称为new_value

Reading out the current value old_value of the respective cell is straight forward:读取相应单元格的当前值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. ---;

How do I update the column z for those cases where x=42 and y=21 with &new_value ?对于x=42y=21的情况,如何使用&new_value更新列z

I would also happy with a data step if it is resasonably fast.如果数据步骤相当快,我也会对它感到满意。 I would like to just modify the table, and not create a new one, because the table is really huge.我只想修改表,而不是创建新表,因为表真的很大。

References参考

Let's do temporary copy of SASHELP.CLASS data for performing an exercise.让我们临时复制 SASHELP.CLASS 数据以进行练习。

data class;
  set sashelp.class;
run;

Select one string value Select 一串值

%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. ---;

Update in SQL is pretty simple, you have got conditions: SQL 中的更新非常简单,您有条件:

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

PROC SQL could be used for update more than one table via VIEW, but it's another question. PROC SQL 可用于通过 VIEW 更新多个表,但这是另一个问题。

In the data step you can use transactions (but data sorting is required, which is not free).在数据步骤中,您可以使用事务(但需要数据排序,这不是免费的)。 Let work with initial copy of CLASS dataset:让我们使用 CLASS 数据集的初始副本:

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

Prepare example transaction data (could be more then one records):准备示例交易数据(可能多于一条记录):

data transaction;
  set class;
  where name='Thomas';
  weight=&new_value;
run;

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

And make update:并进行更新:

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

Keys name, sex and age here are just for example.这里的键名、性别和年龄只是举例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM