DATA Step, Macro, Functions and more

Reshape from wide to long

Accepted Solution Solved
Reply
Contributor
Posts: 65
Accepted Solution

Reshape from wide to long

Hi everyone,

I have a wide dataset similar to following table and want to reshape it to long. Variables' formats: name (character), date(date), new_id and original_id(numeric)

name

Date1

New_id1

Cert_id1

Date2

New_id2

Cert_id2

The columns continue to 5.

 

I used the following code, however it doesn't work and I think the reason is the different formats I have. 

 

DATA long;

SET wide;

ARRAY adate(1:5) date1 - date5 ;

ARRAY anew_id(1:5) new_id1 - new_id5;

ARRAY aoriginal_id(1:5) original_id1 - original_id5;

DO change = 1 to 5 ;

date = adate(change);

new_id =anew_id(change);

original_id =aoriginal_id(change);

OUTPUT;

END;

DROP date1 - date5 new_id1 - new_id5 original_id1 - original_id5;

RUN;

 

I appreciate any and all suggestions.

 


Accepted Solutions
Solution
‎03-19-2016 01:49 AM
Respected Advisor
Posts: 4,919

Re: Reshape from wide to long

The new date variavle doesn't have a format. Just give it a date format with the statement

 

format date yymmdd10.;

PG

View solution in original post


All Replies
Super User
Posts: 19,768

Re: Reshape from wide to long

Your code appears correct to the naked eye, and your methodology is correct.

 

As long as type are the same for the same variable groups that shouldn't be an issue either.  

Please post your log and explain how it's not working. 

Contributor
Posts: 65

Re: Reshape from wide to long

The log window looks fine.

 

NOTE: There were 263872 observations read from the data setWORK.WIDE.

NOTE: The data set WORK.LONG has 1319360 observations and 5 variables.

NOTE: DATA statement used (Total process time): real time 0.11 seconds cpu time 0.10 seconds

 

However in the newe table values are not correct. For example, the new date variable does not contain date values anymore. It's not even date format, YYMMDD10.

 

In the following link, example 5, explains the reshaping with a string variable.

 

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

 

Because of this I assumed that format of the date vriable is problematic. However, I don't know why it should be the case when the type of variable (date) is numeric. (its format is YYMMDD10.)

 

 

Solution
‎03-19-2016 01:49 AM
Respected Advisor
Posts: 4,919

Re: Reshape from wide to long

The new date variavle doesn't have a format. Just give it a date format with the statement

 

format date yymmdd10.;

PG
Contributor
Posts: 65

Re: Reshape from wide to long

Thanks.

☑ This topic is solved.

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

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