BookmarkSubscribeRSS Feed
robertrao
Quartz | Level 8

Hi,

I have a sas code and interested to put inside a macro because I have to repeat the same for every month.

I added the %macro at the top and %mend statements at the bottom as shown???

each time I run the code the last datastep produces two datasets for every month.

The input dataset is the same every time.

About IF ststements inside of macro.......Do I need to add % sign before the conditions or leave as it is as shown below???

Thanks

%macro PSI90 (bgndt=,enddt=,period=);

data want1;

set have;
where DateDischargeKey between &bgndt and &enddt;
run;

/*SOME DATA STEP CODE ........ FORMATS......PROC FREQS .......etc etc*/

/*****
*****
*****
*****
*****
*****
*****
*****/

data location;
set want1;
run;

data final(drop=GenderKey gender_name Abbr Ethnic_Group FinancialClassKey)
set location;
if ATYPE=4 then POINTOFORIGINUB04=put(Admit_Source_Code,$PorignB.);

if AgeAtAdmission=0 and intck('year',Birth_Date2,ADMIT_DATE1) ge 1 then AgeAtAdmission=intck('year',Birth_Date2,ADMIT_DATE1,'c');
if intck('year',Birth_Date2,ADMIT_DATE1) lt 1 then AGEDAY=ADMIT_DATE1-Birth_Date2;
if AGEDAY ge 365 then delete;
run;

libname PSI "C:\AHRQQI";


data psi.allpayer_&period.(drop=Birth_Date2 DISCH_DATE1 ADMIT_DATE1 Row_Source Financial_Class DischargeDispositionID disposition_name
rename=(AgeAtAdmission=age HospitalAccountID=Key LengthOfStay=LOS))
psi.medicare_&period.(drop=Birth_Date2 DISCH_DATE1 ADMIT_DATE1 Row_Source Financial_Class DischargeDispositionID disposition_name
rename=(AgeAtAdmission=age HospitalAccountID=Key LengthOfStay=LOS));

set final;

output psi.allpayer_&period.;

if financial_class='Medicare' then output psi.medicare_&period.;
run;

%mend PSI90;

%PSI90 (bgndt=20130101,enddt=20130131,period=JAN2013_JAN2013); 
%PSI90 (bgndt=20130201,enddt=20130228,period=FEB2013_FEB2013); 
%PSI90 (bgndt=20130301,enddt=20130331,period=MAR2013_MAR2013);
%PSI90 (bgndt=20130401,enddt=20130430,period=APR2013_APR2013);

3 REPLIES 3
RW9
Diamond | Level 26 RW9
Diamond | Level 26

It should be ok, assuming your logic is what you want.  The simplest way to think about the macro pre-processor is that of a text replace/generate function.  So in your example above, take a copy of the macro.  Now go through line by line and replace the parameters with their values.  Is the code how you would expect to see it if you just wanted to run it once?  If so then your good to go.

The %if syntax (along with %do and some other items), is to conditionally generate code.  In your example you are checking a variable within a datastep and directing that data to an output dataset depending on the value, so you want to keep this code as datastep code.

robertrao
Quartz | Level 8

Thanks for the reply RW9...

What is the meaning of "Conditionally generating code"???

secondly, I have a couple of conditions in the last but one dataset also .(as shown above)

Thnx

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Well, conditional - take an example:

%macro Test (Print_Hello_World=No);

     %if "&Print_hello_World."="Yes" %then %do;

          proc report data=hello_world_dataset;    

          run;

     %end;

     %else %do;

          proc report data=sashelp.class;

          run;

     %end;

%mend Test;

%Test (Print_Hello_World=Yes);

%Test ();

Now in the first call the macro variable=Yes.  The %if statement is evaluated as true and the proc report for the hello_world_dataset is generated and sent off to be run, the other proc report does not get generated.

In the second call the default of No is used, hence the if statement evaluates as false and the first proc report never gets generated.  The second one does.

So based of the %if statement result one or the other proc reports are sent to be run from the macro pre-processor, hence you generate code conditionally based on your parameters in this instance.  %do is similar, however when encountered it repeats the code within the block for each iteration, so again generating code programmatically.

You can also do this whole process through datastep which is something I do a fair bit, using the call execute, e.g. the above macro could be written:

data _null_;

     do I='Y','';

          if I='Y' then call execute('proc report data=hello_world_dataset; run;');

          else call execute('proc report dataset=sashelp.class; run;');

     end;

run;

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