data lemon: set pf_lemon end=last; tot_up+up; tot_down+down; ar=tot_up/tot_down; if last: where sub_product='lemon'; run;
can you let me know how to interpret this program.. when are the third to fifth lines getting run? when if last?
if it is not run, what would be tot_up, tot_down and ar?
what are initial values of tot_up and tot_down, null?
Have you tried running this program? It contains a syntax error:
if last:
Please correct your program, run it, then post the complete SAS log including code and notes. This will help you understand what it is doing.
If there is no variable in the source data set pf_lemon that is named Tot_up or Tot_down, and there are variables in the source data set named then following two line would create a running total variable for up and down.
tot_up+up; tot_down+down;
The 5th line calculates a ratio between the two values.
Assuming my assumptions in the first line of my response is accurate the first value of the Tot_ variables is the first value of the up or down variable. Since AR is defined like any other var= (calculation involving other variables); it gets the value from that calculation.
Suggestion, make some dummy data and remove the the "if last", which by the way has a syntax error and the data step would not run. For educational purposes include one or missing values for the up or down variable in the test. Then look at the output.
A statement like: var + something; invokes and implied RETAIN for the variable variable Var.
what is end=last on the second line means?
what is AR mentioned in your response?
@HeatherNewton wrote:
what is AR mentioned in your response?
The variable you provided in the code:
tot_down+down; ar=tot_up/tot_down;
End is just one of the SET statement options that creates a temporary variable for the duration of the data step that provides information from things on the SET statement.
Read details here: https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.4/lestmtsref/p00hxg3x8lwivcn1f0e9axziw57y.htm
It only calculates when if last is commented
it calculates nothing when if last is kept as it is, why?
no actually results are the same where the line for lemon has ar calculated
without or with if last statement
what is the significance of if last statement here?
WHERE is a statement that cannot be made conditional, so it is bad practice to put it after a subsetting IF.
For better understanding, and more efficiency, the code should be like this:
data lemon:
set pf_lemon end=last; /* END= defines a boolean variable that will be true when the last observation is processed */
where sub_product = 'lemon'; /* WHERE tells the dataset engine to filter observations according to the condition */
tot_up + up;
tot_down + down; /* Two SUM statements, causing an implicit RETAIN of the variables */
if last; /* All code after the subsetting IF is executed only when the condition is met */
/* This includes the implicit OUTPUT at the end of the data step iteration! */
ar = tot_up / tot_down; /* I moved this after the IF for efficiency */
run;
Also note that I inserted blanks between operators and operands for better readability.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.