The logic in the attached code block is working with the exception of the X command. Ideally, the X command to execute another SAS program should only run if varfroma=varfromb, but it keeps running even when varfroma ne varfromb. The put statements work properly, but the X command keeps running regardless of whether or not varfroma and varfromb are equal and seems to ignore the if/then/do logic. Can anyone suggest a way to fix this issue? Thanks.
libname datalib '/users/data/lives/here';
data datalib.c;
merge datalib.a datalib.b;
if varfroma=varfromb then do;
put 'BOTH VARIABLES ARE EQUAL';
x 'sas /users/prog/lives/here/runme.sas';
end;
else do;
put 'VARIABLES ARE UNEQUAL';
end;
run;
Afaik the x command can't be executed conditionally. And it is a bit strange to call a sas program in a sas program using os-commands. What are you trying to do? You could, of course, replace the x command with call system.
Afaik the x command can't be executed conditionally. And it is a bit strange to call a sas program in a sas program using os-commands. What are you trying to do? You could, of course, replace the x command with call system.
Thank you, andreas_lds. Using call system worked.
There are a few ways of doing this. Here is one:
libname datalib '/users/data/lives/here';
%let run_pgm =;
data datalib.c;
merge datalib.a datalib.b;
if varfroma=varfromb then do;
put 'BOTH VARIABLES ARE EQUAL';
call symput('run_pgm', '%include "sas /users/prog/lives/here/runme.sas";');
end;
else do;
put 'VARIABLES ARE UNEQUAL';
end;
run;
&run_pgm
First of all, X is not a data step statement, but a global statement. Which means it takes effect immediately when it is encountered in the program text, not when that program text is executed after compilation.
Your code is equivalent to
x 'sas /users/prog/lives/here/runme.sas';
data datalib.c;
merge datalib.a datalib.b;
if varfroma=varfromb then do;
put 'BOTH VARIABLES ARE EQUAL';
end;
else do;
put 'VARIABLES ARE UNEQUAL';
end;
run;
Also mind: if you use call system here (which is a data step call routine), you would execute the external program for EVERY mismatch, which is probably not what you want.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.
Ready to level-up your skills? Choose your own adventure.