Help using Base SAS procedures

how to transpose variables into observations in a data step?

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 8
Accepted Solution

how to transpose variables into observations in a data step?

Hello, I have a single row of data in my data data with probably 20 variables. I'm interested in converting this to separate observations but without using proc transpose. I know that I can do data foo; set my_data; to read in my data set. Then I thought I might be able to do something like input my_var @@; in order to covert the variables into observations but it looks like if I use the input statement, I also need to follow it with the datalines and then the data hardcoded in.

Could someone steer me in the right direction?


Accepted Solutions
Solution
‎12-16-2012 06:07 PM
PROC Star
Posts: 7,471

Re: how to transpose variables into observations in a data step?

Posted in reply to eggman2001

Not sure if this is what you want, but I think it meets your description:

data have;

  input var1-var10 col1-col10;

  cards;

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

;

data want (keep=variable);

  set have;

  array vars _all_;

  do over vars;

    variable=vars;

    output;

  end;

run;

View solution in original post


All Replies
Solution
‎12-16-2012 06:07 PM
PROC Star
Posts: 7,471

Re: how to transpose variables into observations in a data step?

Posted in reply to eggman2001

Not sure if this is what you want, but I think it meets your description:

data have;

  input var1-var10 col1-col10;

  cards;

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

;

data want (keep=variable);

  set have;

  array vars _all_;

  do over vars;

    variable=vars;

    output;

  end;

run;

Occasional Contributor
Posts: 8

Re: how to transpose variables into observations in a data step?

Thanks for your help. I've got one other question.

I have a number of observations sorted by a var1 and I want to find the median of var1 of these observations in a data step. I know I can do n=_n_; which will add the variable with the observation number. But then how can I get the middle observation (if the n is odd) or average the two middle observations (if the n is even). I'd like to assume in my code that I don't know the number of observations that exists in my data set.

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 186 views
  • 0 likes
  • 2 in conversation