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.

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
  • 5 replies
  • 1258 views
  • 3 likes
  • 3 in conversation