Help using Base SAS procedures

As far as SAS statements within a data step are concerned, when does their order make a difference?

Reply
Occasional Contributor
Posts: 5

As far as SAS statements within a data step are concerned, when does their order make a difference?

I'm supposed to synthesize an explanation with  multiple examples in which it DOES make a difference and  multiple examples when it DOES NOT. Quite frankly, I don't really understand the question. 

 

I understand the nature of the DATA step. I understand the uses of the DATA step, but I'm not understanding the question. 

 

How would you interpret this question? 

 

Thank you for your help.

Super User
Super User
Posts: 7,413

Re: As far as SAS statements within a data step are concerned, when does their order make a differen

Its quite simple.  In some situations where a statement appears in a datastep makes no difference to the functionality, in other examples the order is very important.  Here are two examples:

Order is not important:

data want;
  set have;
  ...do some code...
  format variable date9.;
  ...do some more code...;
run;

In this example the format statement is only used once, and applied to the dataset, so it doesn't matter if it is at the beginning, the middle or the end of the code.

 

Order is important:

data want;
  set have;
  if variable=1;
  ...do some other code...
run;

In this example, the position of the if can be critical, consider the case:

data want;
  set have;
  variable=1;
  if variable=1;
run;

In the firs example, you will only get records where variable from the input dataset is 1, in the second example you will get all variables regardless of their value in the contributing dataset, as the if is executed after the assignment.

 

There are many times when this comes up, obviously conditional statements are the easiest to so as that changes the flow of the step, but other statements - position of a set statement for instance, declaration of variables after they are used etc.  can affect the step.

 

Occasional Contributor
Posts: 5

Re: As far as SAS statements within a data step are concerned, when does their order make a differen

Thank you very much, RW9.

 

Can you send me a link to a source where it would elaborate more on this? I have looked for FORMAT statements within DATA steps and I'm finding much more advanced examples, without enough of an explanation. Maybe I should be looking for a different keyword? Are you saying that because it is applying to the whole Dataset and not a subset, or a specific variable, the order is irrelevant? Here I found this:

 

Example 3: Removing a Format

This example disassociates an existing format from a variable in a SAS data set. The order of the FORMAT and the SET statements is important.

data rtest;
   set rtest;
   format x;
run;

Source from: http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000178212.htm#a000...

 

If they're stating that the order is important, then what would happen if the order was reversed?

 

 

RW9, do you have another example that you could elaborate on with regard to the BY statement in a DATA step. My understanding from here is that this could also be another example, but there are no examples on the support website (http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000202968.htm).

Super User
Super User
Posts: 7,413

Re: As far as SAS statements within a data step are concerned, when does their order make a differen

Your best bet is to read the manual - SAS documentation.  It has to do with the Program Data Vector:

http://support.sas.com/documentation/cdl/en/lrcon/62955/HTML/default/viewer.htm#a000961108.htm

 

Think of it this way, if you try to format a variable which doesn't exist at that point...

 

Also note, that you can open SAS, and run the code and move that code line around to see what happens.

Ask a Question
Discussion stats
  • 3 replies
  • 243 views
  • 0 likes
  • 2 in conversation