BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ewhulbert
Calcite | Level 5

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?

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

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;

View solution in original post

5 REPLIES 5
Haikuo
Onyx | Level 15

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

art297
Opal | Level 21

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;

Haikuo
Onyx | Level 15

Thanks, . Good catch! Just Updated.

ewhulbert
Calcite | Level 5

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. 

Haikuo
Onyx | Level 15

No problem at all. Glad to help.

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 2346 views
  • 3 likes
  • 3 in conversation