Building models with SAS Enterprise Miner, SAS Factory Miner, SAS Visual Data Mining and Machine Learning or just with programming

Reshaping from long to wide

Reply
Occasional Contributor
Posts: 16

Reshaping from long to wide

Hello,

 

I have 150 Obs with mutiple statment from 24 months, so its a long list, and I am trying to make it Long to wide.

 

here I have my code, can you please have a look, so see how would I add a Do-Loop on this to make my life easier? Thank you.

 

data Test;

set From;

by ID;

 

retain Statement1-Statement24;

 

array misser Statement1-Statement24;

 

if first.ID=1 then do over misser;

misser =.;

end;

 

if Count=1 then do;

Statement_01=Statement;

end;

if CountForSTMT=2 then do;

Statement_02=Statement;

end;

.

.

.

if CountForSTMT=24 then do;

Statement_24=Statement;

end;

 

if last.ID=1 then output;

 

run;

 

Super User
Posts: 17,840

Re: Reshaping from long to wide

Is this what you're trying to do:

http://www.ats.ucla.edu/stat/sas/modules/widetolong_data.htm

 

If so, consider using a direct PROC TRANSPOSE instead, see the examples here:

http://www.ats.ucla.edu/stat/sas/modules/wtol_transpose.htm

Super User
Posts: 10,500

Re: Reshaping from long to wide

First clarify if you want Statement_02 as used in your assignment or Statement2 as used in your Retain and Array statements.

You don't help yourself by having two different indexing variables count and CountForStmt.

No need for any do loop since your CountForStmt is the index into the array that you need:

 

if CountForStmt> 1 then misser[CountForStmt]=statement;

replace all of the 2 through 24 code that you indicate.

 

Ask a Question
Discussion stats
  • 2 replies
  • 207 views
  • 0 likes
  • 3 in conversation