BookmarkSubscribeRSS Feed
axescot78
Quartz | Level 8

Hello, I'm trying to create a line plot of frequency over time. I would like it to look something like this, except with frequency count rather than percent change on the y-axis.

Capture.PNG

I have all the formatting code, just need the data in the right form. The data looks like this:

data fake_data;
	input pat_id $	pat_type $ visit_date:MMDDYY10.;
	format visit_date MMDDYY10.;
	datalines;
1	A	2/19/2014
2	B	1/23/2010
3	C	11/20/2014
4	B	11/10/2009
5	B	3/3/2008
6	C	7/13/2009
7	B	4/10/2011
8	A	7/28/2006
9	A	2/10/2014
10	C	11/22/2006
11	C	2/10/2011
12	A	3/29/2008
13	B	3/4/2011
14	A	6/27/2006
15	A	9/15/2006
;

I tried using the proc freq statement but couldn't figure out how to get the dates on the x-axis. So I tried proc sgplot but couldn't get each patient on their own plot line. So I tried converting the data to wide as such:

 

 

data fake_data_wide;
	input pat_id $	pat_A $ pat_B $ pat_C $ visit_date:MMDDYY10.;
	format visit_date MMDDYY10.;
	datalines;
1	A			2/19/2014
2		B		1/23/2010
3			C	11/20/2014
4		B		11/10/2009
5		B		3/3/2008
6			C	7/13/2009
7		B		4/10/2011
8	A			7/28/2006
9	A			2/10/2014
10			C	11/22/2006
11			C	2/10/2011
12	A			3/29/2008
13		B		3/4/2011
14	A			6/27/2006
15	A			9/15/2006
;

SAS will plot the y-axis as 'A', 'B', or 'C'. I tried converting the values to '1' but that didn't work either. How can I get the frequencies on the y-axis?

2 REPLIES 2
Reeza
Super User

I don't think you can do this from the raw data, you need to first summarize it. 

Not sure if you need to aggregate to month level, or daily level, or if you want count of distinct patients seen or number of visits. 

 

This should help you get started though. 

 

data fake_data;
infile cards dlm='09'x;
	input pat_id $	pat_type $ visit_date:MMDDYY10.;
	format visit_date MMDDYY10.;
	datalines;
1	A	2/19/2014
2	B	1/23/2010
3	C	11/20/2014
4	B	11/10/2009
5	B	3/3/2008
6	C	7/13/2009
7	B	4/10/2011
8	A	7/28/2006
9	A	2/10/2014
10	C	11/22/2006
11	C	2/10/2011
12	A	3/29/2008
13	B	3/4/2011
14	A	6/27/2006
15	A	9/15/2006
;

proc sql;
create table summary_visits as
select put(visit_date, yymmn6.) as visit_month, 
        pat_type,
       count(distinct pat_id) as distinct_count, 
       count(pat_id) as visit_count
from fake_data
group by calculated visit_month, pat_type;
quit;

proc sgplot data=summary_visits;
series x=visit_month y=distinct_count / group = pat_type;
run;

proc sgplot data=summary_visits;
series x=visit_month y=visit_count / group = pat_type;
run;
ghosh
Barite | Level 11
proc format;
 value $cur   'A'='Euro'
              'B'='Pound'
              'C'='Yen';
run;

proc sgplot data=fake_data;
  vline visit_date/group=pat_type groupdisplay=cluster;
  yaxis min=0;
  format visit_date year4. pat_type $cur.;
run;

 

ghosh_0-1637617627905.png

 

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 2 replies
  • 2249 views
  • 2 likes
  • 3 in conversation