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 |
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 |
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;
Just a heads up you may want a prefix to your variable names and use IDLABEL to get the dates displayed nicely.
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.
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 |
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?
Thank you for the perspective. I will be performing the analysis in ArcGIS.
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!
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.
Ready to level-up your skills? Choose your own adventure.