DATA Step, Macro, Functions and more

Macro looping error

Reply
Contributor
Posts: 59

Macro looping error

I want the following macro to loop through a series of invoices INV06 and choose them.

%macro overall;
data lIB.Drug33 (keep= DRUG_CDE1 S_P_NAME PROVIDER SRVC_PROV_NPI PRSC_PRV FROM_DTE BIRTH STATUS RECIP TENT_PAY SERV_FEE QTY_DISP);
%let status = '1';
%let drug_cde1 = 33;
if status = &status
and drug_cde1 =&drug_cde1 then
set
%do i = 1 %to 2; /*where 1 is the first number and 7 is the last*/

p_claim.Inv06_2010_&i

%end;
;
run;

%mend;

%overall;

I am trying to limit the pull to those with a status of '1' and a drug_cde of 33. However when I run the program I get the following errors;
ERROR: Variable drug_cde1 has been defined as both character and numeric.
ERROR: File WORK.IF.DATA does not exist.
ERROR: File WORK.STATUS.DATA does not exist.
ERROR: The value 1 is not a valid SAS name.
NOTE: Line generated by the macro variable "I".
1 p_claim.Inv06_2010_2
--------------------
557
ERROR: DATA STEP Component Object failure. Aborted during the COMPILATION phase.
ERROR 22-322: Syntax error, expecting one of the following: a name, a quoted string, (, -, :,
;, END, INDSNAME, KEY, KEYS, NOBS, OPEN, POINT, _DATA_, _LAST_, _NULL_.

ERROR 200-322: The symbol is not recognized and will be ignored.

ERROR 557-185: Variable p_claim is not an object.
Super Contributor
Super Contributor
Posts: 365

Re: Macro looping error

Hello Omega1983,

I emulated your code with artificial data and it works fine. It means the problem lies in data. Please submit a sample of you data.

Sincerely,
SPR
Frequent Contributor
Posts: 139

Re: Macro looping error

A number of problems are in your code.

1) your IF statement is before your SET therefore the IF statement is executed before the SET and will never happen. How would SAS know what the value of status and drug_cde is? from what file? Also your logic to just bring in those records where status='1' and drug_cde=33 is way off on how SAS actually processes data.

2) is drug_cde a character data in your input dataset? The error message leads me to believe that

3) since you only want to "bring in" records where status='1' and drug_cde=33 just put that in your where option in the SET statement.

4) a good programming practice is not to put a %LET inside of a DATA STEP.


See below .....


%macro overall;

%local status drug_cde1 ;


%let status = '1';
%let drug_cde1 = 33;


data lIB.Drug33 (keep= DRUG_CDE1 S_P_NAME PROVIDER SRVC_PROV_NPI PRSC_PRV FROM_DTE BIRTH STATUS RECIP TENT_PAY SERV_FEE QTY_DISP);

set %do i = 1 %to 7;
p_claim.Inv06_2010_&i (where=(status=&status and drug_cde1 =&drug_cde1 ))

%end;
;
run;

%mend;

%overall
Super Contributor
Super Contributor
Posts: 365

Re: Macro looping error

Hello Darrylovia,

I agree with your ponts 2 - 4 but not 1. I've tested it works.

Sincerely,
SPR
Frequent Contributor
Posts: 139

Re: Macro looping error

SPR
My bust you are correct

For those in the forum see the below code as a demostration



data s1(drop=s2) s2(drop=s1);
set sashelp.shoes;

inS1=1;
inS2=1;
run;


%macro overall;

data one;

%let Region='Africa';

if Region=&region then
set %do i=1 %to 2;
s&i
%end;
;
run;
%mend overall;
%overall;
Ask a Question
Discussion stats
  • 4 replies
  • 2240 views
  • 0 likes
  • 3 in conversation