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

This is the sample code 

 

data a_k resid;
set akkk;
if mad_command ne 0 then do; output resid and ....; end;
run;

 

So, what I am trying to do is that if mad_command ne 0, then ouput the whole row to resid table AND ALSO OUTPUT the previous row to resid table as well. 

 

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

This is where using the POINT= option of the SET statement helps:

 

data a_k resid;
  set akkk;
  output a_k;

  if mad_command ne 0 then do; 
    output resid;
    p=_n_-1;
    set akkk point=p;
    output resid;
  end;
run;

This program outputs every akkk record to dataset a_k.  If outputs the selected record, and its predecessor to resid.  Note the original preceding record now follows the precipitating record.  If instead you want to preserve the original order, you can:

 

data a_k resid;
  set akkk;
  output a_k;

  if mad_command ne 0 then do p=_n_-1 to _n_;
    set akkk point=p;
    output resid;
  end;
run;
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

View solution in original post

2 REPLIES 2
novinosrin
Tourmaline | Level 20

Hello @eduard1231 

 

Are you asking for this kinda logic?

 


data have;
input var ;
cards;
0
0
4
1
2
;


data want;
set have;
if var ne 0 then do;
output;
n=_n_-1;
set have point=n;
output;
end;
run;
mkeintz
PROC Star

This is where using the POINT= option of the SET statement helps:

 

data a_k resid;
  set akkk;
  output a_k;

  if mad_command ne 0 then do; 
    output resid;
    p=_n_-1;
    set akkk point=p;
    output resid;
  end;
run;

This program outputs every akkk record to dataset a_k.  If outputs the selected record, and its predecessor to resid.  Note the original preceding record now follows the precipitating record.  If instead you want to preserve the original order, you can:

 

data a_k resid;
  set akkk;
  output a_k;

  if mad_command ne 0 then do p=_n_-1 to _n_;
    set akkk point=p;
    output resid;
  end;
run;
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 1826 views
  • 0 likes
  • 3 in conversation