Help using Base SAS procedures

Retain variables when reshaping long to wide

Accepted Solution Solved
Reply
New Contributor
Posts: 3
Accepted Solution

Retain variables when reshaping long to wide

I am attempting to reshape the following long data set to a wide data set and retain individual identifying information.  The data set has missing values.  I can transpose the repeating VAR values but can't seem to carry the TRT and SEX values with the transposition.

Long Data Set

ID     TIME VAR TRT SEX

01     1     1150     1     M

01     2     1158     1     M

02     1     1201     1     F

02     2     1236     1     F

02     3     1240     1     F

03     2     1358     2     M

04     1     1285     2     F

04     2     1280     2     F

04     3     1290     2     F

Desired Wide Data Set

ID     VAR1 VAR1 VAR3      TRT SEX

01     1150     1158            .     1     M

02     1201     1236     1240     1     F

03            .     1358            .     2     M

04     1285     1280     1290     2     F

Scoured previous posts and could not find a similar example.  Thanks for any assistance.


Accepted Solutions
Solution
‎05-16-2015 02:05 PM
Respected Advisor
Posts: 4,932

Re: Retain variables when reshaping long to wide

Try

proc transpose data=long out=wide prefix=var;

by id trt sex notsorted;

id time;

var var;

run;

PG

PG

View solution in original post


All Replies
Solution
‎05-16-2015 02:05 PM
Respected Advisor
Posts: 4,932

Re: Retain variables when reshaping long to wide

Try

proc transpose data=long out=wide prefix=var;

by id trt sex notsorted;

id time;

var var;

run;

PG

PG
New Contributor
Posts: 3

Re: Retain variables when reshaping long to wide

Dear PGStats,

I forgot to specify sex $ when I ran the example in SAS UE.  Nevertheless, your suggestion scaled up nicely to my larger data set. The NOTSORTED option in the BY statement seems to be what I was missing.

Many thanks!!!

Trusted Advisor
Posts: 1,137

Re: Retain variables when reshaping long to wide

Alternatively by arrays in datastep

Thanks,

Jag

Thanks,
Jag
New Contributor
Posts: 3

Re: Retain variables when reshaping long to wide

Posted in reply to Jagadishkatam

Jag,

Thanks so much for your guidance.  After some study your reply has really helped me to better understand array processing in SAS.  Very powerful. 

Best,

John

🔒 This topic is solved and locked.

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

Discussion stats
  • 4 replies
  • 358 views
  • 2 likes
  • 3 in conversation