BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
David_Billa
Rhodochrosite | Level 12

Can someone help me understand the meaning of first. and last. from the below step? In the input dataset it has 900 rows but in the output dataset it has only 200 rows. 

 

My understanding is IF FIRST.PERIOD THEN assign SAN_ACT_KNA = 0 else SAN_ACT_KNA + SAN_ACT. What is the meaning of last.period?

 

DATA WORK.INSIGHTKNASA;
     SET WORK.INSIGHTPLT;
     BY REGION YR PERIOD CAT_NM METRIC_LEVEL_NM TARGETTYPE; 
     IF FIRST.PERIOD THEN SAN_ACT_KNA = 0;
     SAN_ACT_KNA + SAN_ACT;
     IF LAST.PERIOD;
     METRIC_LEVEL_NM = 'KNA';
     SAN_ACT = SAN_ACT_KNA;
     CAT_NM = 'OTHER';
RUN;
1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

FIRST.PERIOD is TRUE when this observation is the first for this particular value of PERIOD (within the current values of REGION and YR).  So either the previous value of PERIOD was different, or the previous value of YR or REGION was different, or it is the first observation in the dataset.

 

LAST.PERIOD is TRUE when this observation is the last for this particular value of PERIOD. So either the next value of PERIOD is different, or the next value of YR or REGION is different, or it is the last observation in the dataset.

 

The IF/THEN statement sets SAN_ACT_KNA to zero when you start a new group.

 

The SUM statement accumulates the values of  SAN_ACT into SAN_ACT_KNA.  If runs for every observation so the value of SAN_ACT on the first observation is not skipped.

 

The subsetting IF statement will stop the step when it is not the last observation for the group.  So only one observation per the groups defined by REGION YR and PERIOD will make it into the output datasets.

View solution in original post

7 REPLIES 7
PeterClemmensen
Tourmaline | Level 20

The 

 

IF LAST.PERIOD;

 

Statement is a Subsetting If Statement. Meaning that anything below it executes only then the condition (last.period = 1) is true. Since there is an implicit output statement at the bottom of the data step, this too executes only when last.period is true. 

 

 

David_Billa
Rhodochrosite | Level 12

@PeterClemmensen Is my understanding right on , IF FIRST.PERIOD THEN assign SAN_ACT_KNA = 0 otherwise SAN_ACT_KNA + SAN_ACT?

LinusH
Tourmaline | Level 20

Not exactly.

If first.period then set SAN_ACT_KNA to 0.

For all observations (inluding when first.period is true) perform SAN_ACT_KNA + SAN_ACT.

So it's not "otherwise" (which would requrie an ELSE statement).

Data never sleeps
yabwon
Onyx | Level 15

It's not exactly "IF FIRST.PERIOD THEN assign SAN_ACT_KNA = 0 otherwise SAN_ACT_KNA + SAN_ACT" since the "SAN_ACT_KNA + SAN_ACT" part is also executed when FIRST.PERIOD=1.

It's rather:

IF FIRST.PERIOD THEN 
do;
  SAN_ACT_KNA = 0;
  SAN_ACT_KNA + SAN_ACT;
end;
else 
  SAN_ACT_KNA + SAN_ACT;
_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



yabwon
Onyx | Level 15

Hi, 

1) Read the documentation page which @PeterClemmensen shared.

2) That data step is "equivalent" to:

DATA WORK.INSIGHTKNASA;
     SET WORK.INSIGHTPLT;
     BY REGION YR PERIOD CAT_NM METRIC_LEVEL_NM TARGETTYPE; 
     IF FIRST.PERIOD THEN SAN_ACT_KNA = 0;
     SAN_ACT_KNA + SAN_ACT;
     IF LAST.PERIOD then do;
       METRIC_LEVEL_NM = 'KNA';
       SAN_ACT = SAN_ACT_KNA;
       CAT_NM = 'OTHER';
       output;
     end;
RUN;
_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



Tom
Super User Tom
Super User

FIRST.PERIOD is TRUE when this observation is the first for this particular value of PERIOD (within the current values of REGION and YR).  So either the previous value of PERIOD was different, or the previous value of YR or REGION was different, or it is the first observation in the dataset.

 

LAST.PERIOD is TRUE when this observation is the last for this particular value of PERIOD. So either the next value of PERIOD is different, or the next value of YR or REGION is different, or it is the last observation in the dataset.

 

The IF/THEN statement sets SAN_ACT_KNA to zero when you start a new group.

 

The SUM statement accumulates the values of  SAN_ACT into SAN_ACT_KNA.  If runs for every observation so the value of SAN_ACT on the first observation is not skipped.

 

The subsetting IF statement will stop the step when it is not the last observation for the group.  So only one observation per the groups defined by REGION YR and PERIOD will make it into the output datasets.

Reeza
Super User

It's summing SAN_ACT by Region, Year, Period and hardcoding these variables:

 

     METRIC_LEVEL_NM = 'KNA';
     SAN_ACT = SAN_ACT_KNA;
     CAT_NM = 'OTHER';

 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 795 views
  • 4 likes
  • 6 in conversation