- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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 |
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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 |
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
Jag
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Just a heads up you may want a prefix to your variable names and use IDLABEL to get the dates displayed nicely.
- Add a new date variable with a yymmdd format so that it's easier to sort your dates correctly
- Add IDLABEL to the PROC TRANSPOSE to have a nicely formatted label
- 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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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 |
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thank you for the perspective. I will be performing the analysis in ArcGIS.