Help using Base SAS procedures

Drop/Keep errors

Reply
Contributor
Posts: 73

Drop/Keep errors

Hi,

I need to run the codes like below many times over.

data abc;
set abc (keep a b c d);
run;

I have a lot of datasets like abc. Trouble is, I don't want to check every dataset that it has variable a, b, c, or d. In most cases, all the datasets have the same variables/columns such as the month when data is avaible. Sometimes, it doesn't have a variable, like when I collect yearly data for 2011 which doesn't have yearly data yet.

When I run the program above, it gives errors like "variable d has never been referenced" and the result dataset has ZERO obs.

My question: How can I run the program above even though the datast might not have variable "d", that is, the program keeps running and reading the data despite absence of variable "d"?
PROC Star
Posts: 7,468

Re: Drop/Keep errors

Posted in reply to smilingmelbourne
You could use something like:
[pre]
data have;
set sashelp.class (drop=height);
run;
proc sql noprint;
select name into :vars separated by " "
from dictionary.columns
where libname eq "WORK" and
memname="HAVE"
and upcase(name) in ("AGE" "HEIGHT" "WEIGHT");
;
quit;
%put &vars.;
data want;
set have (keep=&vars.);
run;
[/pre]
HTH,
Art
---------
> Hi,
>
> I need to run the codes like below many times over.
>
> data abc;
> set abc (keep a b c d);
> run;
>
> I have a lot of datasets like abc. Trouble is, I
> don't want to check every dataset that it has
> variable a, b, c, or d. In most cases, all the
> datasets have the same variables/columns such as the
> month when data is avaible. Sometimes, it doesn't
> have a variable, like when I collect yearly data for
> 2011 which doesn't have yearly data yet.
>
> When I run the program above, it gives errors like
> "variable d has never been referenced" and the result
> dataset has ZERO obs.
>
> My question: How can I run the program above even
> though the datast might not have variable "d", that
> is, the program keeps running and reading the data
> despite absence of variable "d"?
Super User
Posts: 10,020

Re: Drop/Keep errors

Posted in reply to smilingmelbourne
Art.T is right.
Every time before you run this code, you can execute the code offered by Art.T to check the variable list, and put this list where as Art.T do.

Ksharp
Respected Advisor
Posts: 3,799

Re: Drop/Keep errors

Posted in reply to smilingmelbourne
There is an option for that, DKRICOND.

[pre]
782 data y2010(keep=a b c d) y2011(keep=a b);
783 retain a b c d 0;
784 run;

NOTE: The data set WORK.Y2010 has 1 observations and 4 variables.
NOTE: The data set WORK.Y2011 has 1 observations and 2 variables.
NOTE: DATA statement used (Total process time):
real time 0.01 seconds
cpu time 0.01 seconds


785 proc options option=DKRICOND;
786 run;

SAS (r) Proprietary Software Release 9.2 TS2M0

DKRICOND=ERROR Action for DROP/KEEP/RENAME error conditions on input SAS data sets
NOTE: PROCEDURE OPTIONS used (Total process time):
real time 0.03 seconds
cpu time 0.00 seconds


787 data all;
788 set
789 y2010(keep=a b c d)
790 y2011(keep=a b c d)
791 ;
ERROR: The variable c in the DROP, KEEP, or RENAME list has never been referenced.
ERROR: The variable d in the DROP, KEEP, or RENAME list has never been referenced.
792 run;

NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.ALL may be incomplete. When this step was stopped there were 0 observations and 4 variables.
WARNING: Data set WORK.ALL was not replaced because this step was stopped.
NOTE: DATA statement used (Total process time):
real time 0.01 seconds
cpu time 0.01 seconds


793
794 option DKRICOND=NOWARN;
795 data all;
796 set
797 y2010(keep=a b c d)
798 y2011(keep=a b c d)
799 ;
800 run;

NOTE: There were 1 observations read from the data set WORK.Y2010.
NOTE: There were 1 observations read from the data set WORK.Y2011.
NOTE: The data set WORK.ALL has 2 observations and 4 variables.
NOTE: DATA statement used (Total process time):
real time 0.04 seconds
cpu time 0.01 seconds


801 option DKRICOND=ERROR;
[pre]
Ask a Question
Discussion stats
  • 3 replies
  • 4370 views
  • 0 likes
  • 4 in conversation