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

Hello Everyone,

 

I am trying to create a macro for this calculation I am doing for several datasets but just can't seem to get my code to work.  Here's some information you might need if you want to help me.

 

Library name: Diabetes

Datasets = FFYEAR09 FFYEAR10 FFYEAR11 FFYEAR12 FFYEAR13 FFYEAR14 FFYEAR15

 

Here is the original code I want to use in a macro; it works when I do it individually for a dataset.

 

DATA DIABETES.FFYEAR09 (KEEP=RECIPIENT_NBR ALLMEDS TOTALQNT OCTMEDS OCTQNT);

SET DIABETES.FFYEAR09;

BY RECIPIENT_NBR NDC;

RETAIN ALLMEDS TOTALQNT OCTMEDS OCTQNT;

IF FIRST.RECIPIENT_NBR THEN DO;

ALLMEDS = 0;

TOTALQNT = 0;

OCTMEDS = 0;

OCTQNT = 0;

END;

IF FIRST.NDC THEN DO;

ALLMEDS + 1;

TOTALQNT + RX_QUANTITY;

IF MONTH (RX_FILL_DT) = 10 THEN DO;

OCTMEDS + 1;

OCTQNT + RX_QUANTITY;

END;

END;

IF LAST.RECIPIENT_NBR THEN OUTPUT;

LABEL ALLMEDS = 'TOTAL NUMBER OF MEDICATIONS'

TOTALQNT = 'TOTAL QUANTITY OF PILLS'

OCTMEDS = 'TOTAL NUMBER OF MEDICATIONS IN OCTOBER'

OCTQNT = 'TOTAL QUANTITY OF PILLS IN OCTOBER';

RUN;

 

I am fairly new to the world of SAS and this is my first attempt at a macro. This is the macro I developed based on some information I found online for macro beginners.

 

%MACRO TOTALMEDS;

%DO I=1 %TO 7;

DATA = DIABETES.FFYEAR&I (KEEP=RECIPIENT_NBR ALLMEDS TOTALQNT OCTMEDS OCTQNT);

BY RECIPIENT_NBR NDC;

RETAIN ALLMEDS TOTALQNT OCTMEDS OCTQNT;

IF FIRST.RECIPIENT_NBR THEN DO;

ALLMEDS = 0;

TOTALQNT = 0;

OCTMEDS = 0;

OCTQNT = 0;

END;

IF FIRST.NDC THEN DO;

ALLMEDS + 1;

TOTALQNT + RX_QUANTITY;

IF MONTH (RX_FILL_DT) = 10 THEN DO;

OCTMEDS + 1;

OCTQNT + RX_QUANTITY;

END;

END;

IF LAST.RECIPIENT_NBR THEN OUTPUT;

LABEL ALLMEDS = 'TOTAL NUMBER OF MEDICATIONS'

TOTALQNT = 'TOTAL QUANTITY OF PILLS'

OCTMEDS = 'TOTAL NUMBER OF MEDICATIONS IN OCTOBER'

OCTQNT = 'TOTAL QUANTITY OF PILLS IN OCTOBER';

RUN;

%MEND TOTALMEDS;

MPRINT (TOTALMEDS): PROC PRINT DATA=DIABETES.FFYEAR&I;

%TOTALMEDS

 

So something tells me I got the first statement after the "%Do I=1 …" wrong, and I am not sure where "MPRINT" is supposed to go for SAS to tell me exactly where I am messing up. Is there a more complicated macro I'm supposed to use?

 

If anyone can please shed some light on this, I would greatly appreciate your help!

 

I am using SAS 9.2

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

Moreover, the SET statement is missing in your macro. For completeness (and good programming practice), I would also suggest to include a %LOCAL statement.

 

So, to put everything together, the macro should have the following structure:

%macro totalmeds;
%local i;
%do i=9 %to 15;
  data diabetes.ffyear%sysfunc(putn(&i,z2.))(keep=recipient_nbr allmeds totalqnt octmeds octqnt);
  set diabetes.ffyear%sysfunc(putn(&i,z2.));
  by ...
  ...
  run;
%end;
%mend totalmeds;

That said, it seems unusual to me that permanent datasets with detail data are overwritten with datasets containing only aggregated data. (I worked on a diabetes project.) But I assume you know what you're doing and the lost information is still available somewhere else.

View solution in original post

5 REPLIES 5
LinusH
Tourmaline | Level 20
Missing the %end; ?
Data never sleeps
Reeza
Super User

If your data goes from 9 to 15 why are you looping from 1 to 7?

Loop from 9 to 15 and use z2. Format so that the 9 converts to 09. 

 

On top of the solution to add %end. 

 

Astounding
PROC Star

There shouldn't be an equals sign in a DATA statement.  Your original code doesn't use one, but somehow the macro added one:

 

DATA = DIABETES.FFYEAR&I (KEEP=RECIPIENT_NBR ALLMEDS TOTALQNT OCTMEDS OCTQNT);

FreelanceReinh
Jade | Level 19

Moreover, the SET statement is missing in your macro. For completeness (and good programming practice), I would also suggest to include a %LOCAL statement.

 

So, to put everything together, the macro should have the following structure:

%macro totalmeds;
%local i;
%do i=9 %to 15;
  data diabetes.ffyear%sysfunc(putn(&i,z2.))(keep=recipient_nbr allmeds totalqnt octmeds octqnt);
  set diabetes.ffyear%sysfunc(putn(&i,z2.));
  by ...
  ...
  run;
%end;
%mend totalmeds;

That said, it seems unusual to me that permanent datasets with detail data are overwritten with datasets containing only aggregated data. (I worked on a diabetes project.) But I assume you know what you're doing and the lost information is still available somewhere else.

TXSASneophyte
Obsidian | Level 7
Thank you so much for the help! Your solution got the macro humming along just nicely 🙂

Despite the name of the library, this is just exploratory temporary data files I'm working with and creating. My computer has very little free memory that storing the datasets in the "work" library would eat up all my free space. Instead, I put everything on an external hard drive, so my "diabetes" folder is just serving as a temporary folder. But I do appreciate your concern! Since you've worked with diabetes data, I may ask you for your lessons-learned in the future!

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 811 views
  • 0 likes
  • 5 in conversation