BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
jo1
Obsidian | Level 7 jo1
Obsidian | Level 7

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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;

 

View solution in original post

2 REPLIES 2
Reeza
Super User

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;

 

PGStats
Opal | Level 21

Here is a way to

  1. keep the sample definition separate from the data itself and
  2. store the data generation recipe without storing the data 

 

/* 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;
PG

sas-innovate-2024.png

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.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 785 views
  • 2 likes
  • 3 in conversation