BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
sworobec1
Calcite | Level 5

Hello All,

I have a dataset that has column names as dates and I'd like to convert it to a panel structure where those dates are all under one column called "date". I've attached what the data looks like and an desired version of the data. The actual data has a few years worth of months as column names. The 1 and 0s denote whether the car part was working or not. I don't think a simple proc transpose will solve this issue and I don't know where to start. Would appreciate your help. 

Thank you. 

1 ACCEPTED SOLUTION

Accepted Solutions
andreas_lds
Jade | Level 19

Congratulation: you have data in one of the file-formats hardly usable for anything, but data-presentation and to make the problem even more interesting the columns are named in an ill-way. The following code may need some tweaks, but it should be a useful starting point:

/* automatically rename variables to be valid names */
options validvarname=v7;
/* keep-Option to prevent reading columns that have no content,
 * with _: all date-columns stay in the dataset */
proc import file="testdata.xlsx" out=work.have(keep=Car_Id Part _:) replace dbms=xlsx;
run;

/* reshaping */
proc transpose data=work.have out=work.transposed(drop=_label_ rename=(col1=Working)) name=excel_date;
   by Car_Id Part;
   var _:;
run;

/* fixing the date-variable */
data work.want;
   set work.transposed;
   
   length Date 8;
   format Date mmddyys10.;
   
   /* see https://support.sas.com/resources/papers/proceedings/proceedings/sugi29/068-29.pdf */
   Date = input(substr(excel_date, 2) - 21916, best.);
   
   drop excel_date;
run;

View solution in original post

1 REPLY 1
andreas_lds
Jade | Level 19

Congratulation: you have data in one of the file-formats hardly usable for anything, but data-presentation and to make the problem even more interesting the columns are named in an ill-way. The following code may need some tweaks, but it should be a useful starting point:

/* automatically rename variables to be valid names */
options validvarname=v7;
/* keep-Option to prevent reading columns that have no content,
 * with _: all date-columns stay in the dataset */
proc import file="testdata.xlsx" out=work.have(keep=Car_Id Part _:) replace dbms=xlsx;
run;

/* reshaping */
proc transpose data=work.have out=work.transposed(drop=_label_ rename=(col1=Working)) name=excel_date;
   by Car_Id Part;
   var _:;
run;

/* fixing the date-variable */
data work.want;
   set work.transposed;
   
   length Date 8;
   format Date mmddyys10.;
   
   /* see https://support.sas.com/resources/papers/proceedings/proceedings/sugi29/068-29.pdf */
   Date = input(substr(excel_date, 2) - 21916, best.);
   
   drop excel_date;
run;
How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 1 reply
  • 1227 views
  • 0 likes
  • 2 in conversation