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;
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.
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.
@PeterClemmensen Is my understanding right on , IF FIRST.PERIOD THEN assign SAN_ACT_KNA = 0 otherwise SAN_ACT_KNA + SAN_ACT?
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).
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;
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;
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.
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';
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!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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: