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

Hello SAS programmers,

 

I need help converting a table that resembles table 1 (i.e., what I have) to table 2 (i.e., what I need). Thank you in advance for your guidance. 

 

TABLE 1

 

Name Report Date
A 04/02/2020
B 04/02/2020
C 04/02/2020
D 04/02/2020
B 4/3/2020
C 4/3/2020
D 4/3/2020
E 4/3/2020

 

 

TABLE 2

Name 04/02/2020 04/03/2020
A 1 0
B 1 1
C 1 1
D 1 1
E 0 1
1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

HI @PhillipSherlock  A proc freq makes it seemingly simple. 



data have;
input Name$ 	Report_Date:mmddyy10.;
format Report_Date mmddyy10.;
cards;
A 04/02/2020
B 04/02/2020
C 04/02/2020
D 04/02/2020
B 4/3/2020
C 4/3/2020
D 4/3/2020
E 4/3/2020
;

proc freq data=have noprint;
tables name*report_date/sparse out=temp;
run;

proc transpose  data=temp out=want(drop=_:) prefix=Report_Date_;
by name;
id Report_Date;
var count;
run;
Name Report_Date_04_02_2020 Report_Date_04_03_2020
A 1 0
B 1 1
C 1 1
D 1 1
E 0 1

View solution in original post

5 REPLIES 5
Jagadishkatam
Amethyst | Level 16

please try the below code

 

data have;
input Name$ 	Report_Date:ddmmyy10.;
format Report_Date date9.;
vars=1;
cards;
A 04/02/2020
B 04/02/2020
C 04/02/2020
D 04/02/2020
B 4/3/2020
C 4/3/2020
D 4/3/2020
E 4/3/2020
;

proc sort data=have;
by name;
run;

options missing=0;
proc transpose data=have out=want;
by name;
id Report_Date;
var vars;
run;
Thanks,
Jag
Reeza
Super User

Just a heads up you may want a prefix to your variable names and use IDLABEL to get the dates displayed nicely.

 

  1. Add a new date variable with a yymmdd format so that it's easier to sort your dates correctly
  2. Add IDLABEL to the PROC TRANSPOSE to have a nicely formatted label
  3. Add PREFIX to PROC TRANSPOSE to get the D_20200430 as your variable name instead. This makes it easier later on to reference all your dates in an array for example using D:.

 

data have;
input Name$ 	Report_Date:ddmmyy10.;
format Report_Date date9.;
vars=1;
cards;
A 04/02/2020
B 04/02/2020
C 04/02/2020
D 04/02/2020
B 4/3/2020
C 4/3/2020
D 4/3/2020
E 4/3/2020
;

data have2;
set have;
date_name = date;
format date_name yymmddn10.;
run;

proc sort data=have2;
by name;
run;

options missing=0;
proc transpose data=have2 out=want prefix=D_;
by name;
id date_name;
idlabel report_date;
var vars;
run;

Modified from @Jagadishkatam  solution.

 

novinosrin
Tourmaline | Level 20

HI @PhillipSherlock  A proc freq makes it seemingly simple. 



data have;
input Name$ 	Report_Date:mmddyy10.;
format Report_Date mmddyy10.;
cards;
A 04/02/2020
B 04/02/2020
C 04/02/2020
D 04/02/2020
B 4/3/2020
C 4/3/2020
D 4/3/2020
E 4/3/2020
;

proc freq data=have noprint;
tables name*report_date/sparse out=temp;
run;

proc transpose  data=temp out=want(drop=_:) prefix=Report_Date_;
by name;
id Report_Date;
var count;
run;
Name Report_Date_04_02_2020 Report_Date_04_03_2020
A 1 0
B 1 1
C 1 1
D 1 1
E 0 1
PaigeMiller
Diamond | Level 26

Rarely do you need to create dummy variables by yourself. Most SAS data analysis procedures can handle CLASS variables and creates the dummy variables internally, so you don't have to create the dummy variables yourself. So why bother?

--
Paige Miller
PhillipSherlock
Obsidian | Level 7

Thank you for the perspective. I will be performing the analysis in ArcGIS. 

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 5 replies
  • 942 views
  • 5 likes
  • 5 in conversation