## Macro to create number of variables

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

1 ACCEPTED SOLUTION

Accepted Solutions

## Re: Macro to create number of variables

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;``````
--
Paige Miller
3 REPLIES 3

## Re: Macro to create number of variables

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?

--
Paige Miller

## Re: Macro to create number of variables

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;

## Re: Macro to create number of variables

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;``````
--
Paige Miller
Discussion stats
• 3 replies
• 201 views
• 0 likes
• 2 in conversation