DATA Step, Macro, Functions and more

Substitute arithemtic for value in table

Accepted Solution Solved
Reply
Occasional Contributor jo1
Occasional Contributor
Posts: 14
Accepted Solution

Substitute arithemtic for value in table

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.


Accepted Solutions
Solution
‎03-01-2016 10:57 PM
Super User
Posts: 17,837

Re: Substitute arithemtic for value in table

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


All Replies
Solution
‎03-01-2016 10:57 PM
Super User
Posts: 17,837

Re: Substitute arithemtic for value in table

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;

 

Respected Advisor
Posts: 4,651

Re: Substitute arithemtic for value in table

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
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

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