BookmarkSubscribeRSS Feed
moreka
Obsidian | Level 7

I'm not sure if I've worded the question correctly, but what I'm trying to do is use the in() operator to compare the var_dt to the birth_dt or the day after.  I think it's mainly a problem with not using actual values with the in() operator, but I'm not sure how to get the birth_dt and birth_dt+1 to be resolved/evaluated as their internal values. 

 

 

I'm guessing there needs to be something added to or changed regarding the %let statement, but any suggestions would be appreciated!

 

Thanks!

 

 

options minoperator mindelimiter=',';
data tt; set t;
%macro inf;
%let birthrange = (birth_dt, birth_dt+1);
%let i=1;
if (var=1 and var_dt in(&birthrange) then do; infect&i.=101; i=%eval(&i.+1); end;
if (vax=1 and vax_dt in(&birthrange) then do; infect&i.=102; i=%eval(&i.+1); end;
%mend;
%inf;
run; 

Using SAS 9.4.

 

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

What is it your actually trying to achieve.  test data, required output etc.  The code you present is not a good method of working.  Never create macros within other stesp for instance, its just very messy.  Define macro variables at the top of a program outside the other code for readability.

You also cannot mix datastep and macro language like that.  Macro is nothing more than a find replace text mechanism which generates code, it cannot interact with the compiled datastep language as it does not exist at that point.  So where is birthdt coming from, the dataset.  Then use datastep language:

data tt; 
  set t;
  array infect{2} 8.;
  do i=birth_dt to birth_dt+1;
    if var=1 and var_dt in (i) then infect{i}=101; 
    if vax=1 and vax_dt in(i) then infect{i}=102;
  end;
run; 

Do note, its far simpler for you to post working test data, then show what you want than for us to try to guess. 

Astounding
PROC Star

You have several issues here.  For starters, you have an extra set of parentheses ... one on the definition of &BIRTHRANGE, and another following the IN operator.  Regardless, IN does not work on a set of expressions, only on a list of hard-coded values.  And %EVAL does not execute in the middle of a DATA step.  It only executes ahead of time on the original value of &I to determine what statements belong in the DATA step.

 

Luckily, you can avoid all of these issues by avoiding macro language entirely.  Here is the approach:

 

data tt;

set t;

i=1;

array infect {2};

array dates {2} var_dt vax_dt;

if var=1 then do k=1 to dim(dates);

   if (birth_dt <= dates{k} <= birth_dt + 1) then infect{k}=1;

end;

drop k;

run;

 

It's easily expandable if you are actually dealing with more variables.

   

ballardw
Super User

@moreka wrote:

I'm not sure if I've worded the question correctly, but what I'm trying to do is use the in() operator to compare the var_dt to the birth_dt or the day after.  I think it's mainly a problem with not using actual values with the in() operator, but I'm not sure how to get the birth_dt and birth_dt+1 to be resolved/evaluated as their internal values. 

 

 

I'm guessing there needs to be something added to or changed regarding the %let statement, but any suggestions would be appreciated!

 

Thanks!

 

 

options minoperator mindelimiter=',';
data tt; set t;
%macro inf;
%let birthrange = (birth_dt, birth_dt+1);
%let i=1;
if (var=1 and var_dt in(&birthrange) then do; infect&i.=101; i=%eval(&i.+1); end;
if (vax=1 and vax_dt in(&birthrange) then do; infect&i.=102; i=%eval(&i.+1); end;
%mend;
%inf;
run; 

Using SAS 9.4.

 


Assuming that birth_dt is an actual date value and in the data set then the comparison :

if (var=1 and (birth_dt le var_dt le (birth_dt + 1))   ) then ...

should work

 

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
  • 3 replies
  • 2753 views
  • 0 likes
  • 4 in conversation