BookmarkSubscribeRSS Feed
David_Billa
Rhodochrosite | Level 12

I'd like to know what would be the output if I execute the below program with this sample data. I just trying to understand the behavior of First. and Last.

 

DATA WORK.MFGTOT;
     SET WORK.MFG;
     BY YR WK PO;
     IF FIRST.PO THEN DO;
          ZNL_TOT = 0;
          CS_TOT = 0;
          LB_TOT = 0;
     END;
    ZNL_TOT + 1;
    CS_TOT + 1;
    LB_TOT + 1;
     IF LAST.PO;
RUN;

Sample data:

 

PO YR     WK
1   2022   1
1   2022   1
1   2022   1
2   2022   2
2   2022   2
3   2022   3

 

9 REPLIES 9
Tom
Super User Tom
Super User

What part of FIRST and LAST do you not understand?

 

Try saving the values to real variables so you can examine them after the data step to see how they work.

data have;
 length yr wk po 8;
 input PO YR WK ;
cards;
1 2022 1
1 2022 1
1 2022 1
2 2022 2
2 2022 2
3 2022 3
;

data want;
  set have;
  by yr wk po;
  first_yr = first.yr ;
  last_ye = last.yr;
  first_wk = first.wk;
  last_wk = last.wk;
  first_po = first.po ;
  last_po = last.po;
run;

Result:

Obs     yr     wk    po    first_yr    last_ye    first_wk    last_wk    first_po    last_po

 1     2022     1     1        1          0           1          0           1          0
 2     2022     1     1        0          0           0          0           0          0
 3     2022     1     1        0          0           0          1           0          1
 4     2022     2     2        0          0           1          0           1          0
 5     2022     2     2        0          0           0          1           0          1
 6     2022     3     3        0          1           1          1           1          1

Which observation is the first one where the value of YR is 2022?  Which is the last?
Which observation is the first one in the set where YR is 2022 where WK is 1?  Which is the last?

etc.

David_Billa
Rhodochrosite | Level 12

@Tom I just ran your code and then the code which I posted. Result I got for the new variables are,

 

ZNL_TOT    CS_TOT    LB_TOT

3                   3               3

2                   2               2

1                   1               1

 

How the values are same for all the three new variables?

Tom
Super User Tom
Super User

Why would you expect them to be different?  They are set to zero exactly the same way at the same time.  They increment exactly the same way.

David_Billa
Rhodochrosite | Level 12
How does it increment to till 3 ?
Tom
Super User Tom
Super User

It sets it to zero at the start of the group. Then adds one for each observation.  Then deletes all but the last observation.  So it gets to 3 by following this path:  ->0 -> 1->2->3.  You don't see the 1 and 2 because you deleted those observations with the subsetting IF statement.

 

Look up the subsetting IF statement.

https://www.google.com/search?q=%40sas.com+subsetting+if+statement

https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lestmtsref/p1cxl8ifdt8u0gn12wqbji8o5fq1.htm

 

 

Also look up the SUM statement in the documentation to understand why the values increase instead of getting reset to missing every observation.

https://www.google.com/search?q=%40sas.com+sum+statement

https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lestmtsref/n1dfiqj146yi2cn1maeju9wo7ijs.htm

 

 

 

 

Kurt_Bremser
Super User

@David_Billa wrote:

@Tom I just ran your code and then the code which I posted. Result I got for the new variables are,

 

ZNL_TOT    CS_TOT    LB_TOT

3                   3               3

2                   2               2

1                   1               1

 

How the values are same for all the three new variables?


None of the SUM statements is conditional, so all three sums MUST result in the same number.

ballardw
Super User

What I found helpful in understanding First and Last with multiple BY variables was to create variables that hold the values and compare them to the data.

Something like:

DATA WORK.MFGTOT;
     SET WORK.MFG;
     BY YR WK PO;
     Firstyr = First.yr;
     Lastyr = Last.yr;
     Firstwk = First.wk;
     Lastwk = Last.wk;
     Firstpo = First.PO;
     Lastpo = Last.PO;
run;

Values of 1 for True and 0 for False.

 

If you want a more interesting TOTAL that provide different numbers of records and/or additional variables to total, maybe named CS ZNL and LB and use ZNL_TOT = ZNL;

PaigeMiller
Diamond | Level 26

@David_Billa wrote:

I'd like to know what would be the output if I execute the below program with this sample data.


This should never be a question asked here in the SAS Communities. Run the code, and you will know what the output is. You don't have to wait for some here to answer, SAS will tell you.

 


I just trying to understand the behavior of First. and Last.

if first. causes something to happen on the first record of each value of the BY variable specified; in your case your are using if first.PO so it causes something to happen for the first record of each value of PO

 

if last. causes something to happen on the ________ record of each value the BY variable specified (You fill in the blank)

 

So in your code, for the first record of each value of PO, which of the following happen? (choose one)

 

  1. ZNL_TOT, CS_TOT and LB_TOT are assigned a value of zero
  2. ZNL_TOT is assigned a value of zero, CS_TOT and LB_TOT are set to missing
  3. ERRORs are written to the log
  4. all of the above

For the last record of each value of PO, you have the following code

 

if last.po;

 

This causes the record to be written to the data set MFGTOT.

 

How the values are same for all the three new variables?

 

The variables are all handled EXACTLY the same. So why would the values be different?

--
Paige Miller

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 9 replies
  • 966 views
  • 6 likes
  • 5 in conversation