2
votes

In SAS, I have a data set similar to the one below.

ID   TRACT    meanFA    sdFA      medianFA
1    t01      0.56      0.14      0.56
1    t02      0.53      0.07      0.52
1    t03      0.71      0.08      0.71
2    t01      0.72      0.09      0.72
2    t02      0.83      0.10      0.86
2    t03      0.59      0.10      0.62

I am not sure if transpose is the right concept here... but I would want the data to look like the one below.

ID   t01_meanFA  t01_sdFA  t01_medianFA  t02_meanFA  t02_sdFA  t02_medianFA  t03_meanFA  t03_sdFA  t03_medianFA
1    0.56        0.14      0.56          0.53        0.07      0.52          0.71        0.08      0.71
2    0.72        0.09      0.72          0.83        0.10      0.86          0.59        0.10      0.62


proc transpose data=TRACT out=newTRACT;
   var meanFA sdFA medianFA;
   by id;
   id tract meanFA sdFA medianFA;
run;

I have been playing around with the SAS code above, but with no success. Any ideas or suggestions would be great!

3

3 Answers

3
votes

Double transpose is how you get to that. Get it to a dataset that has one row per desired variable per ID, so

ID=1 variable=t01_meanFA value=0.56
ID=1 variable=t01_sdFA value=0.14
...
ID=2 variable=t01_meanFA value=0.72
...

Then transpose using ID=variable and var=value (or whatever you choose to name those columns). You create the intermediate dataset by creating an array of your values (array vars[3] meanFA sdFA medianFA;) and then iterating over that array, setting variable name to catx('_',tract,vname(vars[n])); (vname gets the variable name of the array element).

2
votes

You need 2 transposes. Transpose, use a data step to update then _NAME_ variable, and then transpose again;

proc transpose data=tract out=tract2;
by id tract;
run;

data tract2;
format _name_ $32.;
set tract2;
_name_ = strip(tract) || "_" || strip(_name_);
run;

proc transpose data=tract2 out=tract3(drop=_name_);
by id;
/*With no ID statement, the _NAME_ variable is used*/
var col1;
run;
1
votes

Using example data from this duplicate question.

You can also just do this with a data step.

First, put the maximum sequence number into a macro variable.

proc sql;
select
  max(sequence_no) into : maxseq
from
  have
;
quit;

Create arrays for your new variables, setting the dimensions with the macro variable. Then loop over each visit, putting the events and notes into their respective variables. Output 1 line per visit.

data want(drop=sequence_no--notes);
  do until (last.visit_no);
    set have;
    by id visit_no;
    array event_ (&maxseq);
    array notes_ (&maxseq) $;
    event_(sequence_no)=event_code;
    notes_(sequence_no)=notes;
  end;
  output;
run;