Buddy 2016
Buddy 2016

Reputation: 1

Getting error while running SAS code

I'm getting an error while running the below code. &CNT is 50 and &vars has column names in it.

Each column as some values from 1 to 100. I want to select each column and check the below criteria (%if statement), creating a new variable and assigning the values to it (like free, partially free and not free).

option mlogic mprint;
%macro analysis();
DATA Q2;
SET Q1;
%do i=1 %to &CNT.;
%let segs =%scan(&VARS.,&i.," ");
%IF &SEGS.<=2.5 %THEN &SEGS._R="FREE";
%ELSE %IF (&SEGS.>2.5 AND &SEGS.<5.5) %THEN &SEGS._R="PARTLY FREE";
%ELSE %IF &SEGS.>=5.5 %THEN &SEGS._R="NOT FREE";
/*%PUT &segs.;*/
%end;
RUN;
%MEND;
%analysis();

This is the output I'm getting:

SAS LOG ERROR:

MPRINT(ANALYSIS):   DATA Q2;
MPRINT(ANALYSIS):   SET Q1;
MLOGIC(ANALYSIS):  %DO loop beginning; index variable I; start value is 1;     stop value is 56; by value
  is 1.
MLOGIC(ANALYSIS):  %LET (variable name is SEGS)
MLOGIC(ANALYSIS):  %IF condition &SEGS.<=2.5 is FALSE
MLOGIC(ANALYSIS):  %IF condition (&SEGS.>2.5 AND &SEGS.<5.5) is FALSE
MLOGIC(ANALYSIS):  %IF condition &SEGS.>=5.5 is TRUE
MLOGIC(ANALYSIS):  %PUT &segs.
yr1960
MLOGIC(ANALYSIS):  %DO loop index variable I is now 2; loop will iterate again.
MLOGIC(ANALYSIS):  %LET (variable name is SEGS)
MLOGIC(ANALYSIS):  %IF condition &SEGS.<=2.5 is FALSE
MLOGIC(ANALYSIS):  %IF condition (&SEGS.>2.5 AND &SEGS.<5.5) is FALSE
MLOGIC(ANALYSIS):  %IF condition &SEGS.>=5.5 is TRUE
***NOTE: Line generated by the macro variable "SEGS".
1      yr1961_R
   --------
   22

Upvotes: 0

Views: 60

Answers (1)

DomPazz
DomPazz

Reputation: 12465

You are confusing IF conditions inside the Macro processor versus inside the Data Step. I think this is what you want.

%macro analysis();
DATA Q2;
SET Q1;
%do i=1 %to &CNT.;
%let segs =%scan(&VARS.,&i.," ");
IF &SEGS.<=2.5 THEN &SEGS._R="FREE";
ELSE IF (&SEGS.>2.5 AND &SEGS.<5.5) THEN &SEGS._R="PARTLY FREE";
ELSE IF &SEGS.>=5.5 THEN &SEGS._R="NOT FREE";
/*%PUT &segs.;*/
%end;
RUN;
%MEND;
%analysis();

Macros write code for you. You were comparing the variable Name to the constant values (using string ordering, no less), not the variable values versus the constant values using numbers.

Upvotes: 1

Related Questions