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

Hello all,

 

This is my first post in the SAS forum, hopefully I provide enough information but happy to adapt my question as needed. I am also relatively new to SAS, so my question may be simple.

 

I have a dataset for which I want to sum all rows of the same date into 1 row but separated by another variable which is drug (there are two drugs). I then want to sum all the total number of prescriptions dispensed by each of these two drugs for each day (this is a third column in my dataset). 

 

How would I approach this?

 

Thank you

 

1 ACCEPTED SOLUTION

Accepted Solutions
AlexBennasar
Obsidian | Level 7

Number of incidents by day (in the _freq_ var of the o1 dataset):

 

proc summary data=yourDS;
class molecule time_period;
types molecule*time_period;
output out=o1;
run;

New prescriptions by day and drug:

proc summary data=yourDS;
var prescriptions;
class molecule time_period;
types molecule*time_period;
output out=o2 sum=;
run;

In fact, the second summary is enough, as you have there the _freq_ var too...

View solution in original post

9 REPLIES 9
AlexBennasar
Obsidian | Level 7
An example dataset would be nice, it's difficult to situate yourself without seeing it
mmaximos
Obsidian | Level 7
Good point, I'm uncertain if I can share an actual sample of my dataset, therefore I've created this sample dataset in google sheets that mimics my dataset: https://docs.google.com/spreadsheets/d/15sXZBoHmgRVmBreG9JaPZWVTJ7npGpC5bQm3tGF04fc/edit?usp=sharing . The molecule column contains two drugs, the new prescriptions column contains the number of new prescriptions dispensed, the time period represents the year and month for that drug and new prescriptions dispensed. What I want to be able to do is total all the number of incidents of drug_1 and drug_2 for each day in the time period column and also sum all the total number of new prescriptions for each day by drug. The time period column contains multiple years, so if I try to sum by month then I lose the ability to separate by year.

Thank you for your help, it's very much appreciated.
PaigeMiller
Diamond | Level 26

Some of us (many of us?) will not work with data from spreadsheets, as they can be a security risk and are often unhelpful in providing data in meaningful form for use in SAS code. Please provide (a portion of) the data as working SAS data step code which you can type in yourself (and please test your code to ensure that it works) or which you can create following these instructions.

--
Paige Miller
mmaximos
Obsidian | Level 7

Thank you, I think my question has been answered, but this is helpful information to know for the future. Appreciated. 

AlexBennasar
Obsidian | Level 7

Number of incidents by day (in the _freq_ var of the o1 dataset):

 

proc summary data=yourDS;
class molecule time_period;
types molecule*time_period;
output out=o1;
run;

New prescriptions by day and drug:

proc summary data=yourDS;
var prescriptions;
class molecule time_period;
types molecule*time_period;
output out=o2 sum=;
run;

In fact, the second summary is enough, as you have there the _freq_ var too...

mmaximos
Obsidian | Level 7

This worked great, thank you kindly!

Kurt_Bremser
Super User

Your link is not a SAS dataset, it's an Excel spreadsheet. Your column names are not valid SAS names.

Please post usable data in this form:

data have;
input Molecule $ Total_New_Prescriptions Time_Period $6.;
datalines;
Drug_1 4 201611
Drug_2 1 201611
Drug_2 9 201611
Drug_1 2 201612
Drug_1 4 201612
Drug_1 3 201612
Drug_2 9 201613
Drug_1 2 201613
Drug_2 6 201613
Drug_2 1 201613
;

What does your time period represent? It can't be months, as there ain't a month 13. Are these Julian dates?

mmaximos
Obsidian | Level 7

Thanks for the questions. You are correct, this is not a SAS dataset, which was explained in my initial reply as I cannot link my dataset and I am still learning how to navigate SAS. Thank you for the example of how to post a dataset sample. You are also correct, there is no month 13, that was a flaw in my entry. I think a fellow SAS user may have posted a potential solution, which I will try. If I am still stuck, I will make sure to post a dataset that can be navigated in SAS format if I need to ask further questions.

 

PaigeMiller
Diamond | Level 26

@mmaximos wrote:

Hello all,

 

This is my first post in the SAS forum, hopefully I provide enough information but happy to adapt my question as needed. I am also relatively new to SAS, so my question may be simple.

 

I have a dataset for which I want to sum all rows of the same date into 1 row but separated by another variable which is drug (there are two drugs). I then want to sum all the total number of prescriptions dispensed by each of these two drugs for each day (this is a third column in my dataset). 

 

How would I approach this?

 

Thank you

 


I agree with @AlexBennasar that providing a sample data set, and providing the desired output from that sample data set, would be greatly helpful. Nevertheless, this seems like something PROC SUMMARY will do easily.

--
Paige Miller

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
  • 9 replies
  • 1433 views
  • 0 likes
  • 4 in conversation