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;
Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.
If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website.
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.