BookmarkSubscribeRSS Feed
dm2018
Calcite | Level 5

Apologies - this is a rather basic question although I've spent some time searching and can't seem to find a succinct explanation.

 

I'm interested in knowing why there is no equivalent to 'calculated' (from Proc SQL) within Data Step syntax? Believe this is down to how the code is compiled and processed, Proc SQL being simultaneous and SAS data step being sequential? Does this then mean that when using a 'Where' clause within a datastep, it's not possible to do so using a calculated field? Is there any way around this to increase efficiency or is it only possible to use Proc SQL for this purpose?

 

Many thanks. D.

3 REPLIES 3
Tom
Super User Tom
Super User

The main difference is that in a data step you cannot reference two variables with the same name from different sources like you could in an SQL statement, so there is no need to clarify which variable you mean.

 

Also a data step processes code in sequential order.  So if you want to reference a variable after its value has been "calculated" then just place the reference later in the program flow and it will get the value after it has changed.

Astounding
PROC Star

To focus on one part of your question, the WHERE statement does not use calculated fields.  With exceedingly rare exceptions, it only references fields that already exist in the SAS data set.  I'm not sure if this will confuse matters or suggest greater insight, but here's a test program you can run:

 

data test1;
do k=1 to 20;
   output;
end;
run;

data test2;
set test1;
k=50;
where k < 5;
run;

You will get the 4 observations that have K < 5, before any calculations have been applied.  But the final value of K will be 50, after the calculations have been applied.

Kurt_Bremser
Super User

In a data step, "where" (either as a statement or a dataset option) works exclusively on incoming data. If you want to select on newly created variables, you have to use a subsetting if.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 3741 views
  • 0 likes
  • 4 in conversation