11-05-2014 06:49 AM
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???
%macro PSI90 (bgndt=,enddt=,period=);
where DateDischargeKey between &bgndt and &enddt;
/*SOME DATA STEP CODE ........ FORMATS......PROC FREQS .......etc etc*/
data final(drop=GenderKey gender_name Abbr Ethnic_Group FinancialClassKey)
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;
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));
if financial_class='Medicare' then output psi.medicare_&period.;
11-05-2014 06:58 AM
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.
11-05-2014 07:09 AM
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)
11-05-2014 07:57 AM
Well, conditional - take an example:
%macro Test (Print_Hello_World=No);
%if "&Print_hello_World."="Yes" %then %do;
proc report data=hello_world_dataset;
proc report data=sashelp.class;
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:
if I='Y' then call execute('proc report data=hello_world_dataset; run;');
else call execute('proc report dataset=sashelp.class; run;');