I've a data as follows from one dataset.
| COL_NM | operant | value |
| ACCOUNT_PERIOD_CLOSING_BAL_AMT | ne | . |
| ACCOUNT_PERIOD_END_DT | ne | . |
| REPORTING_METHOD_CD | eq | 'TAX' |
Now I need to apply one condition as below in few programs by reading the above variables so that I no need to write the condition manually.
where ACCOUNT_PERIOD_CLOSING_BAL_AMT ne . or ACCOUNT_PERIOD_END_DT ne . or REPORTING_METHOD_CD eq 'TAX'Could you please help me acheive this task?
Just write the code you want to a file.
filename code temp;
data _null_;
set have end=eof;
if _n_=1 then put 'where ' @ ;
else put ' or ' @;
put col_nm operant value ;
if eof then put ';' ;
run;
You can then use %INCLUDE to add that WHERE statement to any step.
proc freq data=some_other_dataset ;
%include code ;
tables var1 ;
run;
thanks, any other way?
@David_Billa wrote:
thanks, any other way?
How large is the list of conditions? If it is small enough to fit into a macro variable (64K bytes) then just generate a macro variable instead.
proc sql noprint;
select catx(' ',col_nm,operant,value)
into :where separated by ' or '
from have
;
quit;
proc freq ;
where &where ;
tables var1;
run;
Are you talking about a specific ELSE statement in a specific data step? Or the general concept of the ELSE statement?
There is only one data step in this thread. And it has only one ELSE.
if _n_=1 then put 'where ' @ ;
else put ' or ' @;
So _N_=1 will be true only the first iteration of the data step. So this block will either write WHERE or OR at the beginning of the line. The trailing @ on the PUT statement means that a line break is not written.
So the result is a nice readably formatted file.
where a = 100
or b > 5
;
Do you want to apply these conditions in a data step or in Procedures as well?
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.