Hi,
I'm just into the first few weeks of teaching myself SAS so am jumping around learning to do specific things and may be missing what might be basic knowledge. I wanted to generate a random sample from a normal distribution of mean = 0 and sigma to equal a value from another process that I have now in a table. So this works fine:
data N2015;
call streaminit (1);
mu = 0;
sigma =1.456;
do i = 1 to 100;
x2015 = rand("Normal", mu, sigma);
output;
end;
run;
but what I want to do is instead of hard coding the 1.456 into the simga value is to call up the value from the table. I've been about to do this within the sql procedure but I've tried a few things and am not getting the syntax correct within the data procedure.
Appologies if this is a simple questions but I've googled and searched and can't come up in answer.
You have a few options.
1. create a macro variable to hold value and then use that in your formula
2. Provide table to data step to be used
/*Create a macro variable */
proc sql;
select std into :sigma
from have;
quit;
data N2015;
call streaminit (1);
mu = 0;
sigma =1.456;
do i = 1 to 100;
x2015 = rand("Normal", mu, &sigma);
output;
end;
run;
Or assume it's table.
data N2015;
set have;
call streaminit (1);
mu = 0;
sigma =1.456;
do i = 1 to 100;
x2015 = rand("Normal", mu, sigma); /*assumes variable name is sigma*/
output;
end;
drop sigma;*drop other variables not needed;
run;
You have a few options.
1. create a macro variable to hold value and then use that in your formula
2. Provide table to data step to be used
/*Create a macro variable */
proc sql;
select std into :sigma
from have;
quit;
data N2015;
call streaminit (1);
mu = 0;
sigma =1.456;
do i = 1 to 100;
x2015 = rand("Normal", mu, &sigma);
output;
end;
run;
Or assume it's table.
data N2015;
set have;
call streaminit (1);
mu = 0;
sigma =1.456;
do i = 1 to 100;
x2015 = rand("Normal", mu, sigma); /*assumes variable name is sigma*/
output;
end;
drop sigma;*drop other variables not needed;
run;
Here is a way to
/* Sample definition */
data groups;
length id $8 dist $4;
input id dist n mu sigma;
datalines;
One Norm 10 0 1.456
Two Norm 12 0 1.567
Three Norm 14 0 1.678
;
/* Data generation recipe */
data N2015 / view=N2015;
call streaminit (8565);
set groups;
do i = 1 to n;
x2015 = rand(dist, mu, sigma);
output;
end;
keep id x2015;
run;
proc print data=N2015 noobs; run;
Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.
Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.
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.