_NULL_防止覆盖表?

时间:2019-12-12 12:28:15

标签: sas

我遇到了 NULL 可能阻止数据步骤执行的情况。 有人可以看看并确认为什么会发生这种情况。

在SAS EG中运行它:

/*create a TEMP1 table*/
data TEMP1;
input Name $ age score;
cards;
A 10 100
B .  20
C 20 .
D .  .
;
run;

/* step to overwrite WORK.TEMP1 dots with 0 */
DATa _NULL_;
SET TEMP1;
file print;
array a1 _numeric_;
do over a1;
if a1=. then a1=0;
end;
run;

期望是所有带点的数字字段都将被0覆盖。

仅当将DATA NULL 替换为DATA TEMP1

时,它才起作用

有点难题

3 个答案:

答案 0 :(得分:1)

这里有两种不同的方法来替换现有表中的值

  • 使用自身的新副本覆盖整个表
    • data name; set name; …
  • 在现有表中就地修改值
    • data name; modify name; …

示例

1    data class;
2      set sashelp.class;
3    run;

NOTE: There were 19 observations read from the data set SASHELP.CLASS.
NOTE: The data set WORK.CLASS has 19 observations and 5 variables.
NOTE: DATA statement used (Total process time):
      real time           0.17 seconds
      cpu time            0.00 seconds


4
5    data class;     /* output data set named is same as input data set */
6      set class;
7      age = age * 2;
8    run;

NOTE: There were 19 observations read from the data set WORK.CLASS.
NOTE: The data set WORK.CLASS has 19 observations and 5 variables.
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.03 seconds


9
10   data class;      /* output data set name */
11     modify class;  /* is same as modify name, values updated in place */  
12     age = age / 2;
13   run;             /* observations are rewritten (see log) */

NOTE: There were 19 observations read from the data set WORK.CLASS.
NOTE: The data set WORK.CLASS has been updated.  There were 19 observations rewritten, 0
      observations added and 0 observations deleted.
NOTE: DATA statement used (Total process time):
      real time           0.05 seconds
      cpu time            0.00 seconds

第三种方法是将SQL UPDATE语句与基于coalesce的集合一起使用,但是,这不适用于数组处理。

Proc SQL;
  update mydata set 
    a1 = coalesce (a1,0)
  , s2 = coalesce (a2,0)
  … 
  ;

答案 1 :(得分:1)

有些评论可能会有所帮助。基本上,正如其他人指出的那样,for(i in 1:length(filenames)) { text <- read.csv(filenames[i], header = T) //Analysis on text } 不会创建输出数据集,因此您的假设是不正确的。

我怀疑您还错误地使用了FILE,但不知道您要使用该语句做什么。

您还使用了DO OVER循环,该循环从SAS V7开始不推荐使用,因此您不应该在生产代码中使用它。

_NULL_

您可以通过执行此操作来解决它,但是我不建议您使用相同的数据集名称。以后很难调试代码。

DATa _NULL_;*_Null_ means no output data set is created;

SET TEMP1; *input data set means temp1;

file print; *writes to a file named print, no filename statement so no idea what this means to you;

array a1 _numeric_; *creates an array of all numeric values;

 do over a1; *Do over is deprecated as of 20 years ago, it works but I don't recommend using it in production code;
             if a1=. then a1=0; *replaces missing with 0;
end;*ends loop;

*no put statements so nothing is written the file print;

run;

答案 2 :(得分:0)

当您使用data _NULL_而不是data temp1时,您仅从temp1中读取,但是您的更改将不会写入任何地方。这不是基本的SAS功能的难题。仅在不需要写入数据时才使用_NULL_