0

I have a data similar to this (This won't work because of an Array subscript out of range error):

    data test;
    array id {5} (1, 8, 4, 12, 23);
    array a_ {5};
    do i = 1 to 5;
        a_[id[i]] = id[i];
    end;
    run;

what I want to do is, create variables begins with 'a_' and the values of array id. Meaning : a_1, a_8, a_4, a_12, a_23

This will only work if I declare array a_ with 23 members:

    data test;
    array id {5} (1, 8, 4, 12, 23);
    array a_ {23};
    do i = 1 to 5;
        a_[id[i]] = id[i];
    end;
    run;

But then I get lots of missing variables I don't need. I only want the above 5.

How can I achieve that?

1 Answer 1

4

PROC TRANSPOSE is usually the easiest way to do this.

First, make a vertical dataset like so:

data vert;
  array id[5] (1,8,4,12,23);
  do _i = 1 to dim(id);
    varname = cats('A_',id[_i]);
    vvalue = 1; *it is not apparent to me what the value should be in A_12 or whatnot;
    output;
  end;
run;

Then PROC TRANSPOSE makes your desired dataset.

proc transpose data=vert out=want;
  id varname;
  var vvalue;
run;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.