BookmarkSubscribeRSS Feed
HeatherNewton
Quartz | Level 8
data exchange;
set exchange;
retain _from;
by from to;
if first.to then valid_to=.;
else valid_to = _from-1;
output;
_from=valid_from;
where to='HKD';
format _from valid_to date9.;
run;

what does it mean by retain _from by from to? in exchange dataset, there is only 'from','to','valid_from' and 'exchg_rate', 4 columns.. where does _from come from?

very strange the above code...

4 REPLIES 4
Kurt_Bremser
Super User

First, Maxim 1: Read the Documentation (to understand what the statements do)

BY Statement 

RETAIN Statement 

Next, Maxim 3. Know Your Data

Inspect your datasets to see which variables are contained

 

Variables not contained in the incoming dataset(s), but used in the code, will automatically be created by the data step compiler.

 

 

LinusH
Tourmaline | Level 20

RETAIN means _from will keep its value between iterations of the data step.

_from and valid_to are created in this data step.

BY means that the data step expects the input data to be sorted.

 

Strange, no idea since this step is clearly taken out of the context where it executes.

Data never sleeps
s_lassen
Meteorite | Level 14

The placement of the BY statement does not matter that much, it just pertains to the previous SET or MERGE statement.

Your code is equivalent to

data exchange;
set exchange;
by from to;
retain _from;
if first.to then valid_to=.;
else valid_to = _from-1;
output;
_from=valid_from;
where to='HKD';
format _from valid_to date9.;
run;

Which is obviously a lot easier to read.

sbxjld3
Calcite | Level 5
if you could provide a sample dataset, that would help a lot

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

Register now

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 983 views
  • 0 likes
  • 5 in conversation