Tatr
Tatr

Reputation: 13

How to iterate over columns and make a new column on data of existing one?

I am a newbie in SAS. I have a table:

columns:

column1 column2 column3 

with values:

row1: val1.1  val2.1  val3.1

row2: val1.2  val2.2  val3.2

row3: val1.3  val2.3  val3.3

I need to iterate on columns, and for each column create a new one with extended name of existing column and processed values so that the table becomes:

columns:

column1 column1_proc column2 column2_proc column3 column3_proc

row1: val1.1  val1.1.v     val2.1  val2.1v      val3.1  val3.1v

row2:
val1.2  val1.2.v     val2.2  val2.2v      val3.2  val3.2v

row3:
val1.3  val1.3.v     val2.3  val2.3v      val3.3  val3.3v

Are there some functions in SAS language?

Upvotes: 1

Views: 1004

Answers (1)

Tom
Tom

Reputation: 51566

To create a new variable you just need to assign it a value. Since you didn't explain what value you want let's use a simple example of two times the original value.

data want;
  set have;
  column_proc1 = 2 * column1 ;
run;

If you want to perform the same calculations for multiple variables then you should look at the array statement. This will allow you to make a place holder name you can use to refer to a series of variables. You can use the place holder plus an index value to refer to a specific variable.

So again using the simple two times transformation you could define two arrays. One for the original variables and one for the new variables. Note that it works much better with SAS to keep the numeric counters in variable names at the end of the variable name. Then you can use variable lists.

data want;
  set have;
  array old column1 - column3 ;
  array new column_proc1 - column_proc3;
  do i=1 to dim(old);
    new(i) = 2 * old(i) ;
  end;
run;

Upvotes: 1

Related Questions