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-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

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