BookmarkSubscribeRSS Feed
gc236005
Calcite | Level 5

I'm trying to write an IML function where one of the parameters is a dataset and I am trying to avoid having to use the SAS macro language in order to accomplish this task. The dataset I'm looking to pass into the function is called "step2". The following is my attempt.

 

PROC IML;
     START ewma_var(ds,in_var,length);
          USE ds; READ ALL VAR {in_var} INTO r_var;
          n=nrow(r_var);
          s_var=j(n,1,.);

          DO a=1 TO n;
               IF a=1 THEN DO;
                    s_var[a,1] = r_var[a,1];
              END;

              IF a >1 THEN DO;
                   s_var[a,1] = ((r_var[a,1]-s_var[a-1,1])*(2/length+1))+s_var[a-1,1];
              END;

          END;
          CREATE s_var FROM s_var [COLNAME = 's_var']; APPEND FROM s_var;
     FINISH;

     CALL ewma_var(step2,GDP,8);
QUIT;

 

So I want the parameter "ds" to be "step2", the parameter "in_var" to be a variable contained in "step2", and "length" to be a numeric used to calculate the weighting factor.  That way I could then do the following.

 

PROC IML;
     START ewma_var(ds,in_var,length);
          USE ds; READ ALL VAR {in_var} INTO r_var;
          n=nrow(r_var);
          s_var=j(n,1,.);

          DO a=1 TO n;
               IF a=1 THEN DO;
                    s_var[a,1] = r_var[a,1];
              END;

              IF a >1 THEN DO;
                   s_var[a,1] = ((r_var[a,1]-s_var[a-1,1])*(2/length+1))+s_var[a-1,1];
              END;

          END;
          CREATE s_var FROM s_var [COLNAME = 's_var']; APPEND FROM s_var;
     FINISH;

 

     CALL ewma_var(step2,GDP,8);

     CALL ewma_var(step2,GDP,4);

     CALL ewma_var(step2,DEP,2);

     CALL ewma_var(step2,QE,10);
QUIT;

1 REPLY 1
Rick_SAS
SAS Super FREQ

Several comments:

1. You can use parentheses in the USE and CREATE statements to tell SAS/IML that the data set name is not a literal value but is the value contained in a variable.

2. Currently, each call to the EWMA_VAR function overwrites the S_VAR data set. You can pass in an output data set name, if that's not what you want.

3. You need to pass in the name of the data step and the name of the variables as a string by using quotation marks.

4. It looks like you are trying to create an exponentially weighted moving average. If so, see the article, "Rolling statistics in SAS/IML."

 

data step2;
input GDP DEP QE;
datalines;
1 2 3
4 5 6
7 8 9
10 11 12
13 14 15
; 

PROC IML;
  START ewma_var(ds,in_var,length);
       USE (ds); READ ALL VAR in_var INTO r_var; close;
       n=nrow(r_var);
       s_var=j(n,1,.);

       DO a=1 TO n;
            IF a=1 THEN DO;
                 s_var[a,1] = r_var[a,1];
           END;
           IF a >1 THEN DO;
                s_var[a,1] = ((r_var[a,1]-s_var[a-1,1])*(2/length+1))+s_var[a-1,1];
           END;
       END;
       CREATE s_var FROM s_var [COLNAME = 's_var']; APPEND FROM s_var; close;
  FINISH;

  CALL ewma_var("step2","GDP",8);
  CALL ewma_var("step2","GDP",4);
  CALL ewma_var("step2","DEP",2);
  CALL ewma_var("step2","QE",10);
QUIT;

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!
Multiple Linear Regression in SAS

Learn how to run multiple linear regression models with and without interactions, presented by SAS user Alex Chaplin.

Find more tutorials on the SAS Users YouTube channel.

From The DO Loop
Want more? Visit our blog for more articles like these.
Discussion stats
  • 1 reply
  • 576 views
  • 1 like
  • 2 in conversation