0

を使用してデータセットを txt ファイルにエクスポートしようとしています。区切り文字として、データセットには多くの null 数値と char 値が含まれています。これらの値をすべて '' (ここにスペースはありません) に置き換えたいと思います。

以下のコードを使用してこれを実行しようとしましたが、SAS は '' を ' ' (1 つの空白) に変更するようです

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;

オプション missing='' も使用しようとしましたが、うまくいきませんでした。

ありがとう!

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 に答える