SAS PROC Transpose Data

ぐ巨炮叔叔 提交于 2020-01-16 04:50:13

问题


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!


回答1:


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;



回答2:


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).




回答3:


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;


来源:https://stackoverflow.com/questions/31323069/proc-transpose-2-columns-together-in-sas

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!