BookmarkSubscribeRSS Feed
David_Billa
Rhodochrosite | Level 12

I've a data as follows from one dataset.

 

COL_NMoperantvalue
ACCOUNT_PERIOD_CLOSING_BAL_AMTne.
ACCOUNT_PERIOD_END_DTne.
REPORTING_METHOD_CDeq'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?

8 REPLIES 8
Tom
Super User Tom
Super User

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;
David_Billa
Rhodochrosite | Level 12

thanks, any other way?

Tom
Super User Tom
Super User

@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;
David_Billa
Rhodochrosite | Level 12
Could you please help me understand what does the else statement do in the
data step?
Tom
Super User Tom
Super User

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
;
David_Billa
Rhodochrosite | Level 12
I'm talking about the specific else statement which you mentioned in the
previous post.

Would like to know more about the way you tackle the line break with put
statement
PeterClemmensen
Tourmaline | Level 20

Do you want to apply these conditions in a data step or in Procedures as well?

David_Billa
Rhodochrosite | Level 12
Only in a data step
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
  • 8 replies
  • 1781 views
  • 2 likes
  • 3 in conversation