简体   繁体   English

相当于PROC IML中的SAS函数重复

[英]Equivalent to SAS function repeat in PROC IML

I want to define a string in a PROC IML , say "aaaaa" (five "a"). 我想在PROC IML定义一个字符串,比如“aaaaa”(五个“a”)。 In a DATA step, I would use the repeat() function, which creates a string repeating substrings, as explained in the documentation . DATA步骤中,我将使用repeat()函数,该函数创建一个重复子串的字符串,如文档所述

data _null_;
x=repeat('a',4);    /* string with five 'a' */
put x;
run;

However, in SAS/IML, the repeat() function is different : it creates a matrix repeating elements of another one (documentation here ). 但是,在SAS / IML中, repeat()函数是不同的:它创建了一个矩阵重复另一个元素( 这里的文档)。 So if I use this function, I will get a vector with five "a" elements. 所以,如果我使用这个函数,我将得到一个带有五个“a”元素的向量。

proc iml;
x=repeat('a',5);    /* 5 'a' strings */
print x;
quit;

In that example, I could obviously not bother and go directly with : 在那个例子中,我显然不会打扰并直接进入:

x="aaaaa";

But what if I needed a larger string (say 100 "a" for example) ? 但是,如果我需要一个更大的字符串(例如100“a”)怎么办? I could also create it outside of the PROC IML and import it after but there must be a more clever way to address the problem, isn't there ? 我也可以在PROC IML之外创建它并导入它但是必须有一个更聪明的方法来解决这个问题,不是吗?

There is no need to write a loop. 无需编写循环。 Use the ROWCATC function to concatenate the elements across columns: 使用ROWCATC函数跨列连接元素:

proc iml;
N = 10;
x = rowcatc(repeat("a", 1, N));  /* repeat 'a' N times */
print x (nleng(x))[L="Length"];

A slightly harder problem is to concatenate elements and insert some sort of delimiter beteen the elements (blanks, comas, etc). 一个稍微难点的问题是连接元素并在元素(空白,逗号等)之间插入某种分隔符。 That problems is discussed in the article "Convert a vector to a string." “将矢量转换为字符串”一文中讨论了这些问题

As IML works with matrices, that is what you normally would want. 由于IML与矩阵一起使用,这就是您通常想要的。 To get columns instead of rows: 要获取列而不是行:

proc iml;
  x=repeat('a', 1, 5);   
  print x;
quit;

 x
a a a a a

You could convert the vector to string using a loop. 您可以使用循环将矢量转换为字符串。 But in that case it would make more sense to skip repeat and directly use a loop to produce a string: 但在这种情况下,跳过重复并直接使用循环生成字符串会更有意义:

proc iml;
  x="";
  do i = 1 to 5;
    x = x + 'a';
  end;
  print x;
quit;

 x
aaaaa

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM