Help using Base SAS procedures

Conditional output based on last obs

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 8
Accepted Solution

Conditional output based on last obs

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?


Accepted Solutions
Solution
‎10-23-2013 05:53 PM
PROC Star
Posts: 7,363

Re: Conditional output based on last obs

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


All Replies
Respected Advisor
Posts: 3,124

Re: Conditional output based on last obs

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

Solution
‎10-23-2013 05:53 PM
PROC Star
Posts: 7,363

Re: Conditional output based on last obs

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;

Respected Advisor
Posts: 3,124

Re: Conditional output based on last obs

Thanks, . Good catch! Just Updated.

Occasional Contributor
Posts: 8

Re: Conditional output based on last obs

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. 

Respected Advisor
Posts: 3,124

Re: Conditional output based on last obs

No problem at all. Glad to help.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

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