0

我正在尝试使用将数据集导出到 txt 文件中;作为分隔符,数据集有很多空数字和字符值,我想将所有这些值替换为 '' (这里没有空格)。

我尝试使用下面的代码来执行此操作,但似乎 SAS 会将 '' 更改为 ' '(一个空白)

if missing(VAR22) then VAR22='' ;
put VAR22 $ @; 

完整代码:

data _null_;
set  lib.TABLE;                                  
file "/home/user/file.txt" delimiter=';';
format VAR1 $1. ;
format VAR2 $8. ;
format VAR3 $3. ;
format VAR4 $6. ;
format VAR5 $9. ;
format VAR6 $2. ;
format VAR7 $1. ;
format VAR8 $4. ;
format VAR9 $1. ;
format VAR10 $1. ;
format VAR11 BEST12.;
format VAR12 NUMX13.2;
format VAR13 BEST12.;
format VAR14 NUMX13.2;
format VAR15 $1. ;
format VAR16 $2. ;
format VAR17 $3. ;
format VAR18 $3. ;
format VAR19 $4. ;
format VAR20 $20. ;
format VAR21 $1. ;
format VAR22 $1. ;

if missing(VAR1) then VAR1='' ;
put VAR1 $ @; 
put VAR2 $ @;
put VAR3 $ @;
put VAR4 $ @;
put VAR5 $ @;
put VAR6 $ @;
put VAR7 $ @;
put VAR8 $ @;
put VAR9 $ @;
if missing(VAR10) then VAR10='' ;
put VAR10 @;
put VAR11 @;
put VAR12 @;
put VAR13 @;
put VAR14 @;
put VAR15 $ @;
put VAR16 $ @;
put VAR17 $ @;
put VAR18 $ @;
put VAR20 $ @;
put VAR19 $ @;
put VAR21 $ @;
if missing(VAR22) then VAR22='' ;
put VAR22 $ @; 
put ;
run;

我也尝试使用缺少的选项='',但它没有用。

谢谢!

4

2 回答 2

1

SAS does not support null strings; any number of spaces in a character variable with no other character is by definition a character missing. All strings are padded to their full length (whatever is allocated for that entire column) with '20'x (normal space character), and cannot be zero length.

Depending on what you are doing with these variables, there are ways to get them to not affect your downstream processes. For example, trimn will return a zero length string if the variable is all spaces; so var0=trimn(var1)||trimn(var2)||trimn(var3) will return var1||var3 if var2 has only spaces.

If you are doing concatenation, even better is to use cats instead of the || operator, as it automatically removes spaces (including removing all spaces from a missing string). The resulting variable must contain spaces, but the components have theirs removed.

The idea of concatenating strings together is also a good one. Unfortunately I don't think CATX works with completely missing strings, but you could do this perhaps in a macro or something.

data _null_;
set test;
file "c:\temp\test.csv";
outvar = catt(trimn(x1),';',trimn(x2),';',trimn(x3));
put outvar;
run;

Finally, if you're going to be interfacing with another DBMS that does have the concept of a null string, there may be a better solution; post those details.

于 2013-11-14T14:57:05.053 回答
1

尝试这样的事情:

data one;
   length VAR1-VAR4 $1;
   input VAR1-VAR4;
   datalines;
A B C D
E . F G
H . . J
;
run;

data two;
   set one;
   SP=';';
   test1=trim(left(VAR1)) || sp || trim(left(VAR2)) || sp || trim(left(VAR3)) || sp || 
      trim(left(VAR4));
   test1 = compress(test1,' ');
run;

proc print data=two;
run;

我建议你构建一个简单的宏来运行你所有的 VAR1 -- VARXX。

如果您查看变量 test1,您可以看到这就是您想要输出到文本文件的内容。

请注意,如果您的任何 char 变量已经包含空格作为其实际值的一部分,则此代码将删除它们。如果这是一个问题,请告诉我,我可以解决它。

于 2013-11-14T14:02:37.547 回答