1
votes

I am trying to move from SAS into Python and I am facing a few challenges. Specifically, I have a dataset with 301 observations with in wide-format and I am trying to reshape it into long-format. The wide-format dataframe looks like below:

enter image description here

And I want to convert it into a long-format that would look like as below:

enter image description here

The SAS code I have used to convert my dataset from wide to long is:

 data longformat; 
 set wideformat; 
 array arts(*) art1-art10; 
 array cits(*) cit1-cit10; 
 if jobtime =. then jobtime = 11; 
 do year = 1 to dur; 
 if year = dur then promo=event; else promo=0; 
 if year ge jobtime then jobpres=prest2; else jobpres=prest1; 
 art = arts(year); 
 cit = cits(year); 
 output; 
 end; 
run; 

Does anyone have any idea how to make this kind of conversion into Python?

1
I would suggest looking at pandas docs on MultIndex, and then stack/unstack which lets you go from wide to long after you've set up your index. You could also look at docs on Panel. - JohnE

1 Answers

0
votes

From your pictures I can't tell exactly what you're trying to be but in general reshaping data in SAS is most easily done with PROC TRANSPOSE. Here is an example

proc transpose data=wideformat out=longformat;
    by year;
run;

Here is the link to current documentation for PROC TRANSPOSE