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

Hello all,

 

how could I create a table from 

psn_name, application200001, application200002, application200003
a,1,2,3
b,2,3,4

to

psn_name, date, Number_of_applications
a,200001,1
a,200002,2
a,200003,3
b,200001,2
b,200002,3
b,200003,4

an example I need is 

69C51462674E368EF1840C9BE1767F9D.png

could you please give me some suggestions? 

 

thanks in advance.

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

This creates an actual date value that represents the first day of the month. Use any date format to display the date that you would like.

 

data have;
infile datalines dlm=',';
input psn_name $ application200001 application200002 application200003 ;
datalines;
a,1,2,3
b,2,3,4
;
run;

proc transpose data=have out=havetrans;
 by psn_name;
 var  application:;
run;

data want;
   set havetrans;
   date= input(substr(_name_,12,6),yymmn6.);
   format date yymmn.;
   drop _name_;
   rename col1=Number_of_applications;
run;

If the data is not sorted by psn_name it will need to be sorted before the Proc Transpose step.

 

View solution in original post

4 REPLIES 4
ballardw
Super User

This creates an actual date value that represents the first day of the month. Use any date format to display the date that you would like.

 

data have;
infile datalines dlm=',';
input psn_name $ application200001 application200002 application200003 ;
datalines;
a,1,2,3
b,2,3,4
;
run;

proc transpose data=have out=havetrans;
 by psn_name;
 var  application:;
run;

data want;
   set havetrans;
   date= input(substr(_name_,12,6),yymmn6.);
   format date yymmn.;
   drop _name_;
   rename col1=Number_of_applications;
run;

If the data is not sorted by psn_name it will need to be sorted before the Proc Transpose step.

 

Ksharp
Super User
data have;
infile cards dlm=',';
input psn_name $  application200001  application200002  application200003;
cards;
a,1,2,3
b,2,3,4
;
run;
data want;
 set have;
 array x{*} application:;
 do i=1 to dim(x);
  date=compress(vname(x{i}),,'kd');
  number=x{i};
  output;
 end;
keep psn_name date number;
run;
Reeza
Super User

Melt is the R function, transpose is the actual action of flipping a table and the generic term used in most languages. 

PROC TRANSPOSE transposes both wide to long and long to wide. 

Or an array method can be used. 

 

PROC TRANSPOSE

https://stats.idre.ucla.edu/sas/modules/how-to-reshape-data-wide-to-long-using-proc-transpose/

 

Array

https://stats.idre.ucla.edu/sas/modules/reshaping-data-wide-to-long-using-a-data-step/

France
Quartz | Level 8
Hello Reeza, I appreciate your advice paper which helps me a lot.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 3162 views
  • 4 likes
  • 4 in conversation