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

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