BookmarkSubscribeRSS Feed
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
Exactly - then you now understand that the look-up table must have a suitable alternative construct to address the fact that "BETWEEN" is not supported in a SAS DATA step's IF/THEN construct, correct?

And, the next step would be for you to go off and make such changes - to mean that your post to this forum is complete/satisfied, correct?

Scott Barry
SBBWorks, Inc.
deleted_user
Not applicable
Correct, thanks.
deleted_user
Not applicable
Here is what I am trying to do. I have a lookup table of conditions for each outcome (work.test in this illustration, except much bigger in practice) and then I use a data step to generate all the SAS code required and put it into a macro variable 'all_code'. The coding is much simpler if all are 'to the right' of the outcome variable only:

data test;
length outvar condition1 condition2 condition3 condition4 $20;
outvar = "outvar1"; condition1="ge 20"; condition2="eq 1"; condition3="ne 5"; condition4=""; output;
outvar = "outvar2"; condition1=""; condition2=""; condition3="lt 20 "; condition4="eq 1"; output;
outvar = "outvar3"; condition1=""; condition2="eq 1"; condition3="gt 20 "; condition4="eq 1"; output;
run;

data _null_in_practice;
length _t $300;
retain _t condition_names1 - condition_names4;
array conditions{*} $20 condition1 - condition4;
array condition_names{1:4} $20 ("condition1" "condition2" "condition3" "condition4");
set test end=last;
if _n_ = 1 then _t="";
do i = 1 to dim(conditions);
if i=1 then _t=trim(left(_t))!!" if ";
if conditions ne "" then
_t = trim(left(_t))!!" ("!!trim(left(condition_names))!!" "
!!trim(left(conditions))!!") and";
if i = dim(conditions) then do;
_t = substr(_t,1,length(_t) - 3)!!" then "!! trim(left(outvar)) !!" = 1; else "
!! trim(left(outvar)) !! " = 0;";
end;
end;
if last then call symput('all_code',trim(left(_t)));
run;
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
It's now clear that your code is not working yet. You have a reference to an array in the statement below which is illegal:

if conditions ne "" then

I expect you will see this error, given the pasted code syntax:


ERROR: Illegal reference to the array conditions.

Suggest you get your code working as a static program, then consider some alternative "more flexible?" methods (possibly more simply using macro logic).

Scott Barry
SBBWorks, Inc.

Message was edited by: sbb Message was edited by: sbb
deleted_user
Not applicable
Apologies: the forum formatting didn't like square bracket references to array elements. Here it is again:

data test;
length outvar condition1 condition2 condition3 condition4 $20;
outvar = "outvar1"; condition1="ge 20"; condition2="eq 1"; condition3="ne 5"; condition4=""; output;
outvar = "outvar2"; condition1=""; condition2=""; condition3="lt 20 "; condition4="eq 1"; output;
outvar = "outvar3"; condition1=""; condition2="eq 1"; condition3="gt 20 "; condition4="eq 1"; output;
run;

data _null_in_practice;
length _t $300;
retain _t condition_names1 - condition_names4;
array conditions{*} $20 condition1 - condition4;
array condition_names{1:4} $20 ("condition1" "condition2" "condition3" "condition4");
set test end=last;
if _n_ = 1 then _t="";
do i = 1 to dim(conditions);
if i=1 then _t=trim(left(_t))!!" if ";
if conditions{i} ne "" then
_t = trim(left(_t))!!" ("!!trim(left(condition_names{i}))!!" "
!!trim(left(conditions{i}))!!") and";
if i = dim(conditions) then do;
_t = substr(_t,1,length(_t) - 3)!!" then "!! trim(left(outvar)) !!" = 1; else "
!! trim(left(outvar)) !! " = 0;";
end;
end;
if last then call symput('all_code',trim(left(_t)));
run;
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
Suggest bookmarking this page as a favorite for reference - useful reminder for posting special data-strings:

http://support.sas.com/forums/thread.jspa?messageID=27609

Also, with arrays, you can use straight left/right parenthesis characters. Similar to how you can use !! instead of || (two vertical bars) for concatenation.

Are you doing a CALL SYMPUT or a CALL EXECUTE? How does the code get executed after generating the macro variable?

Lastly, I'll ask the question: What are you looking from the forum to get back with this latest shared information? BETWEEN doesn't work with IF....that's a given, but it seems as though you are unwilling to code your look-up table such that the IF statement "expression" string (when coded in parentheses as I demonstrated) must have either a single value to compare (EQ, LT, GE, etc.) or a value-range which must then be split into two components the part that goes left of the "varname" and the other component that is to the right.

Seems pretty clear to me - the look-up table must be altered to accommodate the IF/THEN logic when you have a value-range to use (which would have been used with a BETWEEN, had this been a WHERE statement/clause).

Scott Message was edited by: sbb

SAS Innovate 2025: Register Today!

 

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Hurry, sign up by Dec. 31 to get the 2024 rate of just $495 before it ends! Don't miss out on this incredible savings!


Register now!

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