BookmarkSubscribeRSS Feed
smilingmelbourne
Fluorite | Level 6
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"?
3 REPLIES 3
art297
Opal | Level 21
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"?
Ksharp
Super User
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
data_null__
Jade | Level 19

There is an option for that, DKRICOND.


 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;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 7090 views
  • 1 like
  • 4 in conversation