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 2024

Innovate_SAS_Blue.png

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. 

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.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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