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...
First, Maxim 1: Read the Documentation (to understand what the statements do)
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.
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.
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.
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!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.