Hello SAS community
I am running the code below to create new variables in a data set:
%let NObs = 100;
data t;
do i = 1 to &NObs;
x_1 = rand("Normal");
x_2 = rand("Normal");
.
.
x_n = rand("Normal");
output;
end;
run;
I would like to form a macro to control how many variables to create, i.e., the value of N.
For example, if N=10 I need 10 variables, up to X_10. I need to be able to change N to different values, as needed.
Thank you!
Costas
Still no macro needed
%let nobs=100;
%let nvars=7;
data want;
array x_ {&nvars};
do j=1 to &nobs;
do i=1 to dim(x_);
x_(i)=rand('normal');
end;
output;
end;
drop i j;
run;
No macro needed, just an array.
%let nobs=100;
data want;
array x_ {&nobs};
do i=1 to dim(x_);
x_(i)=rand('normal');
end;
output;
run;
Now, I notice in the code you provided, that it produces one observation with 100 variables. Is that really what you want?
Thank you for this.
No, I want each of the N variables to have 100 observations, drawn from the specified distribution.
.In fact the code I was using is below, and I would like a more flexible way to determine the number of x's (7 in my code below). Thank you again.
%let NObs = 100;
data t;
do i = 1 to &NObs;
x1 = rand("Normal");
x2 = rand("Normal");
x3 = rand("Normal");
x4 = rand("Normal");
x5 = rand("Normal");
x6 = rand("Normal");
x7 = rand("Normal");
output;
end;
run;
Still no macro needed
%let nobs=100;
%let nvars=7;
data want;
array x_ {&nvars};
do j=1 to &nobs;
do i=1 to dim(x_);
x_(i)=rand('normal');
end;
output;
end;
drop i j;
run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.