BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
costasRO
Fluorite | Level 6

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
PaigeMiller
Diamond | Level 26

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

View solution in original post

3 REPLIES 3
PaigeMiller
Diamond | Level 26

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
costasRO
Fluorite | Level 6

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;

PaigeMiller
Diamond | Level 26

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

sas-innovate-white.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.

 

Early bird rate extended! Save $200 when you sign up by March 31.

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1095 views
  • 0 likes
  • 2 in conversation