BookmarkSubscribeRSS Feed
Manol_Jovani
Fluorite | Level 6
I have used SAS for a few years and I consider myself a relatively good user, but have not done matching before, and I need it for a study. 
 
I found your paper, and it looks fantastic:
 
Mine is a simple clinical dataset. I am basically running it as created above, since the variables "age" and "gender" in my dataset are the same. 
 
However, I get an error when I run the code, and I don't understand why. Below I will give the code I use, and the error I get.
 
THE CODE:

data population;
set edge;
if lams_size=1 then casecontrol=1;
else casecontrol=0;
run;

%LET agerange = 5; 
%LET ratio = 2; 
 
DATA cases controls;
   SET population;
   IF casecontrol = 1 THEN OUTPUT cases;
   ELSE OUTPUT controls;
RUN;

PROC FREQ NOPRINT DATA=cases;
   TABLES age*gender/OUT=caseout; 
RUN;

%MACRO sample(v_age, v_gender, v_count); 

   DATA qualify1;
      SET controls;
    WHERE (&v_age-&agerange <= age <= &v_age+&agerange)
      AND
      (gender = "&v_gender");

   case_age = &v_age;
   case_gender = "&v_gender";
   
   SEED = RANUNI(0);
   PROC SORT;
      BY SEED;

   DATA qualify2;
      SET qualify1 NOBS=totobs;
      IF _N_ <= &v_count*&ratio;
      IF &v_count*&ratio <= totobs THEN tag = 'yes';
         ELSE tag = 'no';

   PROC APPEND BASE=matches DATA=qualify2 force; /*new data set ‘matches’ will contain the matched controls*/

   PROC SORT DATA=qualify2 OUT=temp1 (KEEP=uniqueid);
      BY uniqueid;

   PROC SORT DATA=controls OUT=temp2;
      BY uniqueid;

   DATA controls; 
      MERGE temp1(IN=in1) temp2(IN=in2);
      BY uniqueid;
      IF in2 AND NOT in1;

%MEND sample;

DATA _NULL_; /*This data step calls the macro.*/
   SET caseout;
   CALL EXECUTE ('%sample('||age||','||gender||','||count||')');
RUN;
 
THE ERROR
 
The error I get is the following (which then of course influences everything that follows):


NOTE: Numeric values have been converted to character values at the places given by:
(Line):(Column).
254:30 254:40 254:53
NOTE: There were 29 observations read from the data set WORK.CASEOUT.
NOTE: DATA statement used (Total process time):
real time 0.01 seconds
cpu time 0.01 seconds


NOTE: CALL EXECUTE generated line.
1 + DATA qualify1; SET controls; WHERE (25-0 <= age <= 25+0) AND (gender
= "0"); case_age = 25; case_gender = "0"; SEED = RANUNI(0);
ERROR: WHERE clause operator requires compatible variables.

 

 

"25" is simply the youngest age in my data set, which ranges from 25 to 76. I do not understand why it gives the error that "where" needs a compatible variable. Both "age" and "gender" are numeric in my dataset. I double checked to make sure. I am not sure if the "NOTE" above it has somehow converted one of them in a character, and hence the mistake?

 
Any help to figure this out would be greatly appreciated. 
 
Thank you very much
9 REPLIES 9
SASKiwi
PROC Star

It's hard to see what's going on as you have posted your code and warnings / errors separately so we don't know where they are happening. Please post your complete SAS log including messages with the SAS statement at the start - OPTIONS MPRINT; - so we can see what the macro is doing.

Reeza
Super User
Gender is expected to be a character in the code above, not numeric. Age should be numeric.

This tells you have a type issue:
ERROR: WHERE clause operator requires compatible variables.
Sajid01
Meteorite | Level 14

Hello @Manol_Jovani 
Your where clause "WHERE (25-0 <= age <= 25+0) AND (gender= "0");" is the cause of error.

Try changing  (25-0 <= age <= 25+0) . I am giving a sample code below using sashelp.class.

In my example age is between 10 and 12. You can replace 10 with 25 and 12 with 76.

data test;
set sashelp.class(Rename=(sex=gender));
 where (age between 10 and 12) and (gender="M");
 run;

The message " ERROR: WHERE clause operator requires compatible variables" implies the  variables and the values you are comparing with may not be of the same type (for example one may be a numeric and other may be of character types.) Please verify.

ChrisNZ
Tourmaline | Level 20

The message is very clear.

What you say is less clear: How can you say GENDER is numeric, and code GENDER="0"  ?

Remove the quotes.

Sajid01
Meteorite | Level 14

Hello @ChrisNZ 
I am not saying that Gender is numeric or character. I tried to clarify what the error message was in my own way.
Under similar situation, I would find the type of my variables in the where clause.

 

ChrisNZ
Tourmaline | Level 20

> I am not saying that Gender is numeric or character. 

Yes you are


"25" is simply the youngest age in my data set, which ranges from 25 to 76. I do not understand why it gives the error that "where" needs a compatible variable. Both "age" and "gender" are numeric in my dataset. I double checked to make sure. I am not sure if the "NOTE" above it has somehow converted one of them in a character, and hence the mistake?

> Under similar situation, I would find the type of my variables in the where clause.

I don't know what this means.

 

Did removing the quotes fix the error?

 

ballardw
Super User

Did you have code that ran correctly before creating the macro? That should be first thing. The data problem you are having should have been identified/ resolved with the non-macro code debugging.

Manol_Jovani
Fluorite | Level 6
Yes, the code above the macro works fine, there is no problem there. Is that what you are asking?
ballardw
Super User

@Manol_Jovani wrote:
Yes, the code above the macro works fine, there is no problem there. Is that what you are asking?

No. I am asking if the part inside the macro, when tested without any macro coding, ran. The question about whether quotes are needed or not should have been resolved at that time before adding macros.

 

Messages about "conversion"

Numeric values have been converted to character values at the places given by:
(Line):(Column).

is a warning that you are writing code incorrectly. That message says that you are doing something that treats a numeric variable as character. You may not know this yet, but numeric variables when converted to character typically do not create a value that works as you may expect.

Here is a very simple example of the incompatible "where" clause.

data example;;
  /* character value with digits*/
  x='123';
  /* numeric value*/
  y=123;

run;

data result;
  set example;
  where x=y;
run;

Caution:  Writing macros without RUN statements ending Procedures or Data steps is also a "gotcha" just waiting to happen.

You can write macro code quite easily that incorrectly by-passes an "implied" run and have lines from a following procedure or data step thought as part of the current one running. This is extremely easy if one macro calls another. You can spend a LOT of time tracing down such odd seeming problems caused by this small bit. Ask me how I know😓

 

Which is why code you attempt to create inside a macro has to be made without macros and run correctly before ever adding a %macro/ %mend. around it.

 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 9 replies
  • 1630 views
  • 1 like
  • 6 in conversation