BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Caetreviop543
Obsidian | Level 7

Hi,

 

I am trying to add 16 rows to a table, where variable x1 goes from 1 to 5 by .25 increments. x2 is the square of x1, and the remaining variables are the same (either 0 or the mean).

 

For example,

 

ID            x1 x2 x3 x4 x5 x6 x7

.               .   .   .    .   .   .    .

.               .   .   .    .   .   .    .

.               .   .   .    .   .   .    .

Dum1      1  1  0   0  0  0  27.9

Dum125 1.25 1.56 0 0 0 0 27.9

Dum150 1.50 2.25 0 0 0 0 27.9

Dum175 1.75 3.06 0 0 0 0 27.9

...

 

I originally used the input statement to create the data (above), and then appended it my original dataset. Does anyone know a better way?

 

Thanks in advance,

 

Emily

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

@Caetreviop543 wrote:

Hi,

 

I am trying to add 16 rows to a table, where variable x1 goes from 1 to 5 by .25 increments. x2 is the square of x1, and the remaining variables are the same (either 0 or the mean).

 

For example,

 

ID            x1 x2 x3 x4 x5 x6 x7

.               .   .   .    .   .   .    .

.               .   .   .    .   .   .    .

.               .   .   .    .   .   .    .

Dum1      1  1  0   0  0  0  27.9

Dum125 1.25 1.56 0 0 0 0 27.9

Dum150 1.50 2.25 0 0 0 0 27.9

Dum175 1.75 3.06 0 0 0 0 27.9

...

 

I originally used the input statement to create the data (above), and then appended it my original dataset. Does anyone know a better way?

 

Thanks in advance,

 

Emily


How do we know when the other variables are 0 or the mean? Mean of what?

And where does the 27.9 for x7 come from?

 

I would start with something like:

data want;
   length id $ 10.;
   do x1=1 to 5 by .25;
      id = cats('Dum',x1*100);
      x2= x1*x1;
      x3=0;
      x4=0;
      x5=0;
      x6=0;
      x7=27.9;
      output;
   end;
run;

If this needs to be at the end of another data set then something like:

 

data final;
   set have
       want;
end;

Or use Proc Append but that requires all the variables in the "want" set to already exist in the have data set.

 

View solution in original post

4 REPLIES 4
ballardw
Super User

@Caetreviop543 wrote:

Hi,

 

I am trying to add 16 rows to a table, where variable x1 goes from 1 to 5 by .25 increments. x2 is the square of x1, and the remaining variables are the same (either 0 or the mean).

 

For example,

 

ID            x1 x2 x3 x4 x5 x6 x7

.               .   .   .    .   .   .    .

.               .   .   .    .   .   .    .

.               .   .   .    .   .   .    .

Dum1      1  1  0   0  0  0  27.9

Dum125 1.25 1.56 0 0 0 0 27.9

Dum150 1.50 2.25 0 0 0 0 27.9

Dum175 1.75 3.06 0 0 0 0 27.9

...

 

I originally used the input statement to create the data (above), and then appended it my original dataset. Does anyone know a better way?

 

Thanks in advance,

 

Emily


How do we know when the other variables are 0 or the mean? Mean of what?

And where does the 27.9 for x7 come from?

 

I would start with something like:

data want;
   length id $ 10.;
   do x1=1 to 5 by .25;
      id = cats('Dum',x1*100);
      x2= x1*x1;
      x3=0;
      x4=0;
      x5=0;
      x6=0;
      x7=27.9;
      output;
   end;
run;

If this needs to be at the end of another data set then something like:

 

data final;
   set have
       want;
end;

Or use Proc Append but that requires all the variables in the "want" set to already exist in the have data set.

 

Caetreviop543
Obsidian | Level 7

Great! That works. I just meant that x3-x7 are either binary or continuous, where binary variables are set to 0 and continuous variables (x7) are set to the mean. Basically, I’m setting all my covariates to the same value. 27.9 corresponds to the mean age for my sample.

 

Is it possible to create another variable (x0) with the same values as x1? Ideally the data would look like this:

 

ID              x0  x1 x2 x3 x4 x5 x6 x7 x8

DUM100    1  1  1  1  0  0  0  0  27.9

DUM125    1.25  1.25  1.56  1.56  0  0  0  0  27.9

DUM150    1.50  1.50  2.25  2.25  0  0  0  0  27.9

 

However, the code below groups x1 by x0, resulting in:

 

ID              x0  x1 x2 x3 x4 x5 x6 x7

DUM100    1  1  1  1  0  0  0  27.9

DUM125    1  1.25  1  1.56  0  0  0  0  27.9

DUM150    1  1.50  1  2.25  0  0  0  0  27.9

 

data want;

   length id $ 10.;

   do x0=1 to 5 by .25;

   do x1=1 to 5 by .25;

      id = cats('Dum',x1*100);

      x2=x0*x0;

      x3=x1*x1;

      x4=0;

      x5=0;

      x6=0;

      x7=0;

      x8=27.9;

      output;

   end;

   end;

run;

 

Thanks for your help!

ChrisNZ
Tourmaline | Level 20

> Does anyone know a better way?

 

If the base table already exists, creating a small table and appending it is the best way.

Otherwise you recreate the whole table just to add a few rows.

Caetreviop543
Obsidian | Level 7

Ok, that makes sense. I figured it was the best way, just wanted to make sure.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 4 replies
  • 5320 views
  • 0 likes
  • 3 in conversation