I have a dataset that I only want to use if the last obs of a certain variable meets a condition. Otherwise I want to delete the data. I tried something like this:
data xyz;
set xyz end=last;
if last then do;
if variable = 1 then output;
else delete;
end;
run;
When I run this it works in determining if the last obs of a variable meets the condition, but it only outputs the last observation. I want it to output the entire data set for use in other processes, is there an easy way to do this?
I'm sure that , in the data step example, meant to include the file name in the first use of the set statement.
However, in addition to a hash, you could also use call execute if no file is wanted. e.g.,
data _null_;
if _n_=1 then do;
point=nobs;
set sashelp.class nobs=nobs point=point;
if name ne 'Mary' then stop;
end;
else call execute('data dontwant;set sashelp.class;run;');
run;
data _null_;
if _n_=1 then do;
point=nobs;
set sashelp.class nobs=nobs point=point;
if name ne 'William' then stop;
end;
else call execute('data want;set sashelp.class;run;');
run;
data want;
if _n_=1 then do;
point=nobs;
set have nobs=nobs point=point;
if variable ne 1 then stop;
end;
set have;
run;
This may not be what you want, it gives you either a complete copy of table or an empty one, but it does generate a new table one way or the other.
Haikuo
Update: if you need a whole table or no table (not even an empty one), Hash can do that:
data _null_;
if 0 then set have;
declare hash h(dataset:'have', multidata:'y');
h.definekey(all:'y');
h.definedata(all:'y');
h.definedone();
declare hiter hi('h');
rc=hi.last();
if variable=1 then rc=h.output(dataset:'want');
stop;
run;
Message was edited by: haikuo bian
I'm sure that , in the data step example, meant to include the file name in the first use of the set statement.
However, in addition to a hash, you could also use call execute if no file is wanted. e.g.,
data _null_;
if _n_=1 then do;
point=nobs;
set sashelp.class nobs=nobs point=point;
if name ne 'Mary' then stop;
end;
else call execute('data dontwant;set sashelp.class;run;');
run;
data _null_;
if _n_=1 then do;
point=nobs;
set sashelp.class nobs=nobs point=point;
if name ne 'William' then stop;
end;
else call execute('data want;set sashelp.class;run;');
run;
Thanks for all of your help. Sorry Haikuo Bian, it only lets me give one correct answer. I picked Arthur's because it was easier to implement in my program, but yours works well too.
No problem at all. Glad to help.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.