DATA Step, Macro, Functions and more

variable selection : keep if exists

Accepted Solution Solved
Reply
Contributor
Posts: 24
Accepted Solution

variable selection : keep if exists

I have several databases, one per geographical variables, that I want to append in the end. I am doing some data steps on them. As I have large databases, I select only the variables I need when I first call each table. But on tables in which one variable always equals 0, the variable is not in the table.

So when I select my (keep=var) in a for loop, it works fine if the variable exists, but it produces an error in the other case, so that these tables are ignored.

do i=1 to 10 ;

data temp;

     set area_i(keep= var1 var2);

run;

proc append base=want data=temp force;

run;

Is there a simple way to tackle that ?


Accepted Solutions
Solution
‎10-30-2014 08:52 AM
Super User
Posts: 10,041

Re: variable selection : keep if exists

DKRICOND=ERROR | WARN | WARNING | NOWARN

DKROCOND=ERROR | WARN | WARNING | NOWARN

View solution in original post


All Replies
Super User
Posts: 7,842

Re: variable selection : keep if exists

You can suppress the generation of an ERROR with options nodsfnerr;

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Contributor
Posts: 24

Re: variable selection : keep if exists

Posted in reply to KurtBremser

I did not find any occurence of your option :smileyconfused:

Super User
Posts: 7,842

Re: variable selection : keep if exists

Brain fart. SAS(R) 9.2 Language Reference: Dictionary, Fourth Edition describes it, but it is used to suppress errors caused by nonexisting datasets.

Xia Keshan has the correct solution.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Solution
‎10-30-2014 08:52 AM
Super User
Posts: 10,041

Re: variable selection : keep if exists

DKRICOND=ERROR | WARN | WARNING | NOWARN

DKROCOND=ERROR | WARN | WARNING | NOWARN

Contributor
Posts: 24

Re: variable selection : keep if exists

It worked, thanks a lot !

Super User
Super User
Posts: 7,977

Re: variable selection : keep if exists

You could also create an empty template dataset, and set this with your data so the variables you require are never missing:

proc sql;

     create table TEMPLATE (VAR1 char(200),VAR2 char(200));

quit;

data temp (keep=var1 var2) ;

     set area_i template;

run;

That way temp will always have var1 and var2.

Contributor
Posts: 24

Re: variable selection : keep if exists

I am afraid it will add too much time, as it adds one dataset creation. I have millions of observations, so this might matters.

Super User
Super User
Posts: 7,977

Re: variable selection : keep if exists

Sorry, I was going on the example you had given.  If you are dealing with large amount of datasets then you want a better way of working.  Check the metadata and then generate the program from that.  Whilst there are ways to get around warnings and such like, they do come out for a reason.  IMO I would try to fix these rather than remove them from the log.  E.g.

proc sql;

     select     distinct NAME

     into         :TABLE1 separated by " "

     from       SASHELP.VCOLUMN

     where     LIBNAME="WORK"

          and    MEMNAME="DATASET"

          and    NAME in ("VAR1","VAR2");

     select     distinct NAME

     into         :TABLE2 separated by " "

     from       SASHELP.VCOLUMN

     where     LIBNAME="WORK"

          and    MEMNAME="DATASET"

          and    NAME in ("VAR1","VAR2");

/* Note, probably easier ways to get the macro lists I am typing this quickly */

quit;

data want;

     set table1 (keep=id &table1.)

          table2 (keep=id &table2.);

run;

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 8 replies
  • 411 views
  • 6 likes
  • 4 in conversation