Desktop productivity for business analysts and programmers

Check for existing column

Posts: 73

Check for existing column

Hello EG Users,

I am writing simple computed columns in SAS EG that calculate Total = sum(t1.var1,t1.var2)

However, sometimes the dataset may end up not having var1 at all since this dataset is being put together after multiple filters, transpose and merges.

So is there a way to determine if the column being used in the calculation exists before it is used in the equation?

EXIST function is useful to verify if dataset exists, and not quite helpful in this scenrio.

Any reference to var1 will give me error that says

ERROR: Column var1 could not be found in the table/view identified with the correlation name T1

So does SAS Eg provide a way to check if column exists?



Super User
Posts: 5,378

Re: Check for existing column

Not that I know of. If you want to check this, you need some kind of macro'ish logic somewhere in your flow.

Perhaps it's easier to code in some way that you always will have var1, like having a template table which append to, or doing something like

data want;

set template



..Your  EG task...

Can't tell how to implement this since I know nothing of the rest of the flow, amount of user written vs Tasks etc.

Data never sleeps
Super User
Super User
Posts: 7,682

Re: Check for existing column

Rather than hardcoding the list of variables to add into the sum and having to check existence, why not just start with an identifiable column range, say call all the ones you do want to sum have a prefix of SUM_, then use sql to create the macro variable with these in:

proc sql;

     select     distinct NAME

     into          :LIST_OF_SUM separated by ','

     from          DICTIONARY.COLUMNS

     where         LIBNAME="your libname here"

          and        MEMNAME="your dataset here"

          and          substr(NAME,1,3)="SUM";


%put &LIST_OF_SUM.;

/* Possible output:     SUM_COL1, SUM_COL2... */

That way only variables which exist will be in your macro variable.

Posts: 73

Re: Check for existing column

alright thanks!

Ask a Question
Discussion stats
  • 3 replies
  • 1 like
  • 3 in conversation