BookmarkSubscribeRSS Feed
fengyuwuzu
Pyrite | Level 9

I use the following code to simulate some data but do not know how to output. 

when I use print x it printed only 3 columns data, not 4 columns. Please help. 

proc iml;
call randseed(1);
n = 1000;
Shape = {74, 104, 38, 24};
x = RandDirichlet(n,Shape);
print x;

 

 

6 REPLIES 6
PeterClemmensen
Tourmaline | Level 20

Like this?

 

proc iml;
call randseed(1);
n = 1000;
Shape = {74, 104, 38, 24};
x = RandDirichlet(n,Shape);
print x;

create MyData var {x};
append;
close MyData;
quit;

 

fengyuwuzu
Pyrite | Level 9

yes, but this only output one column of data with 3000 rows. 

 

I want to output a data set with 1000 rows and 4 columns 

Reeza
Super User

You have to list the columns but otherwise the process is the same. 

See this blog post:

https://blogs.sas.com/content/iml/2011/04/18/writing-data-from-a-matrix-to-a-sas-data-set.html

 


@fengyuwuzu wrote:

yes, but this only output one column of data with 3000 rows. 

 

I want to output a data set with 1000 rows and 4 columns 


 

PeterClemmensen
Tourmaline | Level 20

Why do you expect this to be 4 columns and not one? Just trying to figure out what you want to do.

fengyuwuzu
Pyrite | Level 9

Sorry, I did not make it clear in my original post. 

 

My data has 4 categories, so each category has a percentage, and the 4 percentages add up to 1. 

Now I want to simulate a random sample of these 4 percentages, using randdirichlet, 

So I want to get random sample data set, which will have 1000 rows of 4 columns, each column is the 

percentage of one category. 

 

I guess I did not specify the shape correctly, so I only got 3 columns of percentages when I print it?

I have total (74+104+38+24) obs, and each number represents the count of one category. 

 

Shape = {74, 104, 38, 24};

 

I figured out how to get the 4th percentage, but I am not sure if this is the right way:

proc iml;
call randseed(1);
n = 1000;
Shape = {50, 100, 150, 200};				/* use simple numbers for testing */
x = RandDirichlet(n,Shape);  				/* x is a 1000 x 3 matrix, not 1000 x 4 */
samplemean=mean(x);  					/* check mean */
print samplemean;

varnames='percentage1':'percentage3';
create MyData from x[colname=varnames]; 		/* mydata has only first 3 percentages columns */
append from x;
close MyData;
quit;

data mydata2;
set mydata;
percentage4=1-(percentage1+percentage2+percentage3); /* to get the 4th percentage */
run;
Reeza
Super User

From the documentation:

The RANDDIRICHLET function generates a random sample from a Dirichlet distribution, which is a multivariate generalization of the beta distribution.

The input parameters are as follows:

N

is the number of observations to sample.

Shape

is a $1 \times (p+1)$ vector of shape parameters for the distribution, $\mbox{Shape}[i]>0$.

The RANDDIRICHLET function returns an $N \times p$ matrix that contains $N$ random draws from the Dirichlet distribution.

 

Note that Shape is supposed to be p+1 vector, while it returns a N x p matrix. 

 


@fengyuwuzu wrote:

Sorry, I did not make it clear in my original post. 

 

My data has 4 categories, so each category has a percentage, and the 4 percentages add up to 1. 

Now I want to simulate a random sample of these 4 percentages, using randdirichlet, 

So I want to get random sample data set, which will have 1000 rows of 4 columns, each column is the 

percentage of one category. 

 

I guess I did not specify the shape correctly, so I only got 3 columns of percentages when I print it?

I have total (74+104+38+24) obs, and each number represents the count of one category. 

 

Shape = {74, 104, 38, 24};

 

I figured out how to get the 4th percentage, but I am not sure if this is the right way:

proc iml;
call randseed(1);
n = 1000;
Shape = {50, 100, 150, 200};				/* use simple numbers for testing */
x = RandDirichlet(n,Shape);  				/* x is a 1000 x 3 matrix, not 1000 x 4 */
samplemean=mean(x);  					/* check mean */
print samplemean;

varnames='percentage1':'percentage3';
create MyData from x[colname=varnames]; 		/* mydata has only first 3 percentages columns */
append from x;
close MyData;
quit;

data mydata2;
set mydata;
percentage4=1-(percentage1+percentage2+percentage3); /* to get the 4th percentage */
run;

 

https://support.sas.com/documentation/cdl/en/imlug/65547/HTML/default/viewer.htm#imlug_modlib_sect01...

 

 

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 1731 views
  • 0 likes
  • 3 in conversation