BookmarkSubscribeRSS Feed
HeatherNewton
Quartz | Level 8

 

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?

 

 

 

 

 

9 REPLIES 9
SASKiwi
PROC Star

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.

ballardw
Super User

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.

HeatherNewton
Quartz | Level 8

what is end=last on the second line means?

 

HeatherNewton
Quartz | Level 8

what is AR mentioned in your response?

ballardw
Super User

@HeatherNewton wrote:

what is AR mentioned in your response?


The variable you provided in the code:

tot_down+down;
ar=tot_up/tot_down;
ballardw
Super User

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

 

 

 

 

HeatherNewton
Quartz | Level 8

It only calculates when if last is commented

 

it calculates nothing when if last is kept as it is, why?

HeatherNewton
Quartz | Level 8

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?

 

Kurt_Bremser
Super User

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.

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
  • 9 replies
  • 618 views
  • 0 likes
  • 4 in conversation