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

Hello,

 

I have the following row of data:

Sub   Date                Phase

01     2016-06-06      Before

01     2016-06-24      During

01     2016-07-01      During

01     2016-08-19      During

01     2016-08-26      After

 

I need to keep the first and last During to have the data look like this:

Sub   Date                Phase

01     2016-06-06      Before

01     2016-06-24      During

01     2016-08-19      During

01     2016-08-26      After

 

I tried to use the first. and last variable like below:

 

DATA JR.SV_021;
SET SV_02;
BY PHASE;
IF first.PHASE THEN OUTPUT;
IF last.PHASE THEN OUTPUT;
run;

 

but it does not work for me.  I appreciate your help very much in advance!

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Describe how "doesn't work": Error, unexpected output, no output.

And show the code ran and the log.

View solution in original post

7 REPLIES 7
Astounding
PROC Star

A small change should get this to work.  Change the BY statement to:

 

BY PHASE NOTSORTED;

 

That will permit your BY statement even though the data are not in order by Phase.

 

Also note, if your actual data set is larger and might contain more than one SUB value, you may need to use:

 

BY SUB PHASE NOTSORTED;

 

Either way, the rest of the program can remain the same.

zz
Calcite | Level 5 zz
Calcite | Level 5

Tried, but somehow, it did not work:(

ballardw
Super User

Describe how "doesn't work": Error, unexpected output, no output.

And show the code ran and the log.

zz
Calcite | Level 5 zz
Calcite | Level 5

Hi ballardw, It worked!  I checked and one libref was not correct for input data.  Once that's corrected, it all worked.  Thank you very much!

zz
Calcite | Level 5 zz
Calcite | Level 5

Hi ballardw,

 

One problem, rows are duplicated, if phase changed in the next row, eg: the first row was duplicated.

ballardw
Super User

Replace

IF last.PHASE THEN OUTPUT;

with

IF last.PHASE and not (First.Phase) THEN OUTPUT

Saidurga1
Calcite | Level 5

data durga;

input Sub 2. Date:yymmdd10. Phase $ 24-30;

infile datalines;

cards;

01 2016-06-06 Before

01 2016-06-24 During

01 2016-07-01 During

01 2016-08-19 During

01 2016-08-26 After

;

run;

proc sort data=durga out=class;

by Phase;

run;

data durga_;

set class;

by phase;

if first.Phase=1 or last.phase=1;

run;

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 7 replies
  • 6628 views
  • 1 like
  • 4 in conversation