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

Many thanks, this works, now my problem is that I want to sum the number of prescriptions in each month by item_code

That is, there should be a final table that has columns period, item_code and prescriptions.

There should be rows of which the first rown is the column name, the second should be 01SEP2013 and the third should be 01OCT2013.

The observations in the cells should be the sum of prescriptions in each month by (unique) item_code  

 item_codesum of prescriptions
1-Sep-13  
1-Oct-13  

 

Proc sql;

create table homebase.dummy3 as

select item_code, sum(prescriptions) as prescriptions_sum

from homebase.dummy2

group by item_code, period;

quit;

proc print data=homebase.dummy3;

run;

1 ACCEPTED SOLUTION

Accepted Solutions
ChrisNZ
Tourmaline | Level 20

Just add the wanted column:

 

Proc sql;

create table homebase.dummy3 as

select item_code, period, sum(prescriptions) as prescriptions_sum

from homebase.dummy2

group by item_code, period;

quit;

proc print data=homebase.dummy3;

run;

View solution in original post

5 REPLIES 5
Phil_from_PGA
Calcite | Level 5

Sorry, I mean the following should be the result with the last column being the sum of prescriptions:

 

item_code

period

(No column name)

03387G

9/01/2013

14214

03387G

10/01/2013

3146

03390K

9/01/2013

91

03390K

10/01/2013

325567

03391L

9/01/2013

7

03391L

10/01/2013

44889

03393N

9/01/2013

867

03393N

10/01/2013

52

ChrisNZ
Tourmaline | Level 20

Just add the wanted column:

 

Proc sql;

create table homebase.dummy3 as

select item_code, period, sum(prescriptions) as prescriptions_sum

from homebase.dummy2

group by item_code, period;

quit;

proc print data=homebase.dummy3;

run;

Phil_from_PGA
Calcite | Level 5

thanks for replying Chris. I ran the code but realise now there is something wrong in an earlier step where i create a precursor table called dummy2 and when I proc print the table it looks fine. However when I open it, the message I get is Data set has nil observations.

 

I've attached my original data which is imported to SAS as dummy1 which then is converted to dummy2 (to make the date column SAS based) and then dummy3 to try and do the sum of prescriptions

 

Here's my code:

 

Libname pointing to homebase folder;

libname homebase 'C:\Homebase';

 

* View SAS table called dummy1 ;

proc print data=homebase.dummy1;

run;

 

* this creates a new table and formats the period column to be a SAS DATE column;

data homebase.dummy2;

set homebase.dummy1;

input period : ddmmyy10. (item_code patient_cat prescriptions) ($);

format period yymmd.;

datalines;

run;

proc print data=homebase.dummy2;

run;

 

Proc sql;

create table homebase.dummy3 as

select item_code, period, sum(prescriptions) as prescriptions_sum

from homebase.dummy2

group by item_code, period;

quit;

proc print data=homebase.dummy3;

run;

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Post your test data in the form of a datastep using a code window - its the {i} above the post area.  Office files are not safe to download off the net.

ChrisNZ
Tourmaline | Level 20

You shouldn't have a set statement (read a sas table) and input+datalines (read free text) statement together.

 

I suspect you want set.

 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 4445 views
  • 0 likes
  • 3 in conversation