简体   繁体   English

从 SAS 中的随机数值向量生成图

[英]Generate a plot from random numerical vector in SAS

I'm quite new to SAS, I have learned about SGplot, Datalines, IML and randgen.我对 SAS 很陌生,我了解了 SGplot、Datalines、IML 和 randgen。 I'd like to simply generate a random data for a simple scatter plot.我想简单地为一个简单的散点图生成一个随机数据。

/* declaring manually a numeric list */
    data my_data;
        input x y @@;
        datalines;
        1 1 0 8 1 6 0 1 0 1 2 5
        0 3 1 0 1 0 1 4 2 4 1 0
        0 0 0 1 1 2 1 1 0 4 1 0
        1 4 1 0 1 3 0 0 0 1 0 1
        1 0 1 1 2 3 0 2 1 4 2 6
        2 6 1 0 1 1 0 1 2 8 1 3
        1 3 0 5 1 0 5 5 0 2 3 3
        0 1 1 0 1 0 0 0 0 3
        ;
        run;
        
        
        proc sgplot data=my_data;
        scatter x=x y=y; 
        run;

Now I would like in a similar manner to generate a vector of random numbers, such as:现在我想以类似的方式生成一个随机数向量,例如:

proc iml;
N = 94;
rands = j(N,1);
call randgen(rands, 'Uniform');  /* SAS/IML 12.1 */
run;

and afterwards to transfer the vector as datalines and afterwards pass it into the SGplot.然后将向量作为数据线传输,然后将其传递到 SGplot。 Can somebody please demonstrate how to do it?有人可以演示如何做吗?

Since you want to pass it directly to datalines , use the submit and text substitution options in IML.由于您想将其直接传递给datalines ,因此请使用 IML 中的submit和文本替换选项。 This passes rands as an Nx1 vector into the datalines statement, allowing you to read it as one big line.这会将rands作为 Nx1 向量传递到 datalines 语句中,允许您将其作为一条大线读取。

proc iml;
    N = 94;
    rands = j(N,1);
    call randgen(rands, 'Uniform');  /* SAS/IML 12.1 */

    submit rands;
        data my_data;
            input x y @@;
            datalines;
            &rands
            ;
        run;
    endsubmit;

quit;

proc sgplot data=my_data;
    scatter x=x y=y;
run;

Note you'll need to double your size of N to get exactly 94, otherwise you will have 47. This is because it is reading each pair on the same line before moving to the next line.请注意,您需要将 N 的大小加倍才能得到准确的 94,否则您将有 47。这是因为它在移动到下一行之前读取同一行上的每一对。 eg:例如:

1 2 3 4

x = 1 y = 2
x = 3 y = 4 

Source: Passing Values into Procedures (Rick Wicklin)资料来源: 将值传递给过程 (Rick Wicklin)

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

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