SAS 中是否有用于定义数组中字母序列的简写?
许多语言都拥有一种可以轻松做到这一点的机制,我想 SAS 也是如此,尽管我找不到它的参考资料。
例如,在 RI 中可以做
> x <- letters[1:4]
> x
[1] "a" "b" "c" "d"
Run Code Online (Sandbox Code Playgroud)
在 Python 中,一种方法是
>>> import string
>>> list(string.ascii_lowercase[:4])
['a', 'b', 'c', 'd']
Run Code Online (Sandbox Code Playgroud)
在 SAS 中,我目前必须明确列出字母,
data _null_;
array letters (4) $ _temporary_ ('a', 'b', 'c', 'd');
do i = 1 to hbound(letters);
put letters(i);
end;
run;
Run Code Online (Sandbox Code Playgroud)
您可以使用COLLATE()来生成单字节字符的字符串。如果您不知道所需字符块开头的 ASCII 代码,请使用该RANK()函数。
因此,如果您只想从 'a' 开始四个字符,您可以这样做。
length str $4 ;
str = collate(rank('a'));
Run Code Online (Sandbox Code Playgroud)
或者您也可以使用可选的第二个参数来COLLATE()指定所需的字符数。
length str $4 ;
str = collate(rank('a'),rank('a')+vlength(str)-1);
Run Code Online (Sandbox Code Playgroud)
不需要“数组”,只需使用变量即可。
data _null_;
length str $4 ;
str = collate(rank('a'));
do i=1 to vlength(str);
ch = char(str,i);
put i= ch= :$quote. ;
end;
run;
Run Code Online (Sandbox Code Playgroud)
结果:
i=1 ch="a"
i=2 ch="b"
i=3 ch="c"
i=4 ch="d"
Run Code Online (Sandbox Code Playgroud)