3

I am currently working on converting a SAS script to R. As I am relatively new to SAS, I am having a hard time understanding the following statement -

VARS=date id sales units 
/* create lag event variable names to be used in the RETAIN statement */
%let vars_l = lag_%sysfunc(tranwrd(&vars,%str( ),%str( lag_))); 

Here, date, id etc. are all variables present in my current data set. I understand the function tranwrd is used to replace a value by another value in a Character variable. In this case, it creates new items as -

vars_l = lag_date lag_id lag_sales lag_units

Am I right? What is vars_l? Is it a list? Or are these variables that are added to my dataset? Also what is the use of lag before %sysfunc in the code below for?

%let vars_l = lag_%sysfunc(tranwrd(&vars,%str( ),%str( lag_))); 

Are lagged variables created at all or just variables with no values prefixed with lag_ is created?

I don't have access to SAS or the datasets to try and check the result. Any help on this would be great. Thanks!

RHelp
  • 815
  • 2
  • 8
  • 23

1 Answers1

4

The following code is basically creating macro variables to hold a list of variables to process. (Note: macros in SAS are mere text replacements!)

%let VARS=date id sales units ;
/* create lag event variable names to be used in the RETAIN statement */
%let vars_l = lag_%sysfunc(tranwrd(&vars,%str( ),%str( lag_))); 

If you run the following code (to see what exactly is stored in VARS & vars_l macros you see the following in the log:

31         %put VARS::&VARS.;
SYMBOLGEN:  Macro variable VARS resolves to date id sales units
VARS::date id sales units
32         %put VARS_l::&VARS_L.;
SYMBOLGEN:  Macro variable VARS_L resolves to lag_date lag_id lag_sales lag_units
VARS_l::lag_date lag_id lag_sales lag_units

In R the equvialent would be the following:

VARS<-c("date", "id", "sales", "units" )
vars_l<-paste("lag_",VARS, sep="")

The second vars_l macro assignment is just adding lag_ to the begining of each space delimited value in VARS macro variable. Since the first value does not begin with a space, if you omit the lag_ at the begining of %let vars_l = lag_%sysfunc(tranwrd(&vars,%str( ),%str( lag_))); you will get the following stored in vars_l: date lag_id lag_sales lag_units

From the code I can see there are no variables created just yet, but you should find a data step later on which does that. The mention of RETAIN statement in the comments suggests just that.

  • True. The following lines does have a merge command and **RETAIN VARS_L** Statement. But what does that mean? Four new lag columns are created but what are the cell values in these variables? – RHelp Jan 29 '14 at 12:01
  • @RHelp: you really need to post the full SAS code ( with the retain statement). It depends on what what values the RETAIN varibles are initialised with. and how the assignment happens in the later part of the code. –  Jan 29 '14 at 12:14