Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String SAS 9.4字符函数-为什么一个单元格会返回意外结果?_String_Date_Sas_Substring_String Length - Fatal编程技术网

String SAS 9.4字符函数-为什么一个单元格会返回意外结果?

String SAS 9.4字符函数-为什么一个单元格会返回意外结果?,string,date,sas,substring,string-length,String,Date,Sas,Substring,String Length,我正在处理一些字符串变量,某些单元格使用子字符串和长度函数返回意外值。这些单元格包含字符格式的日期,因为我需要在将它们转换为SAS日期之前进行一些操作,因为它们来自Excel文件的性质。以下是一个例子: 有: Obs\u orig 1 4/3 2 12/16 3 1/13 4 6/2 5 3/10 6.5/4 2014年10月7日 想要: Obs原始长度子长度1子长度2 1 4/3 3 4/ 2 12/16 5 12 3 1/13 4 1/ 4 6/2 3 6/ 5 3/10 4 3/ 65/4

我正在处理一些字符串变量,某些单元格使用子字符串和长度函数返回意外值。这些单元格包含字符格式的日期,因为我需要在将它们转换为SAS日期之前进行一些操作,因为它们来自Excel文件的性质。以下是一个例子:

有:
Obs\u orig
1 4/3
2 12/16
3 1/13
4 6/2
5 3/10
6.5/4
2014年10月7日

想要:
Obs原始长度子长度1子长度2
1 4/3 3 4/
2 12/16 5 12
3 1/13 4 1/
4 6/2 3 6/
5 3/10 4 3/
65/435/
7 10/14 5 10

我正在使用以下代码:

data want;
set have;
_strip=strip(_orig);
_sub_1_2=substr(_strip,1,2);
_length=length(_strip);
run;
这就是我得到的。差异以粗体显示。
Obs原始长度子长度1子长度2
14/35
2 12/16 5 12
3 1/13 4 1/
4 6/2 3 6/
5 3/10 4 3/
65/45
7 10/14 5 10


这两种情况都是SAS在长度应为3时计算长度为5的情况。在这两种情况下,子字符串派生变量的值都是空的。如果在代码中使用compress()、trim()或trimn(),而不是strip(),则结果相同。感谢您提供的任何帮助

听起来您的数据中可能包含无法打印的字符。如果您
将原始$hex放入到日志,您看到了什么?应为:342F33200

152  data want;
153   length orig $5;
154   orig='4/3';
155   len=length(orig);
156   put orig= len=;
157   put orig hex.;
158  run;

orig=4/3 len=3
342F332020
要清除不可打印的字符,可以尝试:

_strip=compress(orig,,'kw');

我似乎很清楚,您的变量具有前导空格或屏幕上类似空格的其他前导字符。因此,对于OBS=6,字符串的值更像是长度为5的
“5/4”
,前两个字符看起来都像空格。如果新的
\u sub\u 1\u 2
变量的LENGTHN()不是0,则该变量具有一些非打印字符。可能类似于'A0'X,某些网页使用它作为不间断空格或制表符('09'X)

我怀疑您不需要前两个字符,而是在使用
/
作为分隔符时需要第一个单词。可以使用LEFT()或STRIP()函数删除前导空格。或压缩()以删除其他垃圾。因此,您可以将COMPRESS()与
k
d
修饰符一起使用,以仅保留数字和斜杠

data want;
  set have;
  length first $5 ;
  first = scan(compress(_orig,'/','kd'),1,'/');
run;

完美的不可打印字符是由所有者/用户添加的单元格内换行符。为了使您提供的压缩功能正常工作,我添加了一个额外的逗号:_strip=compress(orig,,'kw');再次感谢你,很高兴成功了。两个逗号接得好。更新了答案。