0

I have a data file dat.txt like

            no chn math eng
            1  93   88  74
            2  65   33  90
            3  78   69  83

I want a new data like

            id subject score
            1     chn    93
            1    math    88
            1     eng    74
            2     chn    65
            2    math    33
            2     eng    90
            3     chn    78
            3    math    69
            3     eng    83

Using R, I can achieve that by

            dat1=read.table("dat.txt",head=T)
            dat1=dat1[,-1]
            n=dim(dat1)[1]
            p=dim(dat1)[2]
            id=gl(p,n)
            subject=rep(names(dat1),n)
            score=as.vector(t(dat1))
            dat2=data.frame(id,subject,score)

I am new to SAS and I try

            DATA dat1;
            INFILE 'dat.txt' firstobs=2;
            INPUT  no chn math eng;
            RUN;

Then I don't know what else to do...

Joe
  • 62,789
  • 6
  • 49
  • 67
John Stone
  • 635
  • 4
  • 13
  • Also, using `tidyr` in R: `df <- df %>% gather("subject",value = "score", 2:4)` – yeedle Nov 02 '16 at 15:18
  • That question has a couple of options, I suggest the PROC TRANSPOSE option, or look around for other similar question/answers - this comes up a lot. – Joe Nov 02 '16 at 15:18
  • Tks. I have an answer according to [http://stackoverflow.com/questions/35631395/how-to-reshape-data-wide-to-long?rq=1](http://stackoverflow.com/questions/35631395/how-to-reshape-data-wide-to-long?rq=1), and the answer to my question is proc transpose data=dat1 out=dat2(rename=(_name_=subject COL1=score no=id)); by no; run; PROC print DATA=dat2; RUN; – John Stone Nov 02 '16 at 16:08

0 Answers0