Raven
Raven

Reputation: 859

Keep Variables Created by Macro

I have the following code where I rename column names; I would like to keep only the variables created by the macro. I do realize I can drop the old variables but am curious if there is a keep option I can place inside the macro.

So for example, in the datastep, I would want to keep only the variable that start with '%transform_this(JUNE19)';

Thanks!

 %macro transform_this(x);
 &x._Actual=input(Current_Month, 9.0);
 &x._Actual_Per_Unit = input(B, 9.);
 &x._Budget=input(C, 9.);
 &x._Budget_Per_Unit=input(D, 9.);
 &x._Variance=input(E, 9.);
 &x._Prior_Year_Act=input(G, 9.);
 Account_Number=input(H, 9.);
 Account_Description=put(I, 35.);
 &x._YTD_Actual=input(Year_to_Date, 9.);
 &x._YTD_Actual_Per_Unit=input(L, 9.);
 %mend transform_this;

 data June_53410_v1;
 set June_53410;
 %transform_this(JUNE19);
 if Account_Description='Account Description' then DELETE; 
 Drop Current_Month B C D E G H I Year_to_Date L M N;
 run; 

Upvotes: 1

Views: 286

Answers (3)

Richard
Richard

Reputation: 27508

Add two sentinel variables to the data step, one before the macro call and one after. Use the double dash -- variable name list construct in a keep statement and drop the sentinels in the data step output data set specifier drop= option.

data want (drop=sentinel1 sentinel2); /* remove sentinels */
  set have;
  retain sentinel1 0;
  %myMacro (…)
  retain sentinel2 0;
  … 
  keep sentinel1--sentinel2;  * keep all variables created by code between sentinel declarations;
run;

Name Range Lists

Name range lists rely on the order of variable definition, as shown in the following table:

Name Range Lists

Variable List Included Variables
x -- a all variables in order of variable definition, from variable x to variable a inclusive
x -NUMERIC- a all numeric variables from variable x to variable a inclusive
x -CHARACTER- a all character variables from variable x to variable a inclusive

Note: Notice that name range lists use a double hyphen ( -- ) to designate the range between variables, and numbered range lists use a single hyphen to designate the range.

Upvotes: 2

Nickolay
Nickolay

Reputation: 32063

am curious if there is a keep option I can place inside the macro.

You can definitely use keep in your macro:

%macro transform_this(x);
    keep &x._Actual &x._Actual_Per_Unit
         &x._Budget &x._Budget_Per_Unit
         &x._Variance &x._Prior_Year_Act
        Account_Number Account_Description 
        &x._YTD_Actual &x._YTD_Actual_Per_Unit
    ;

    &x._Actual=input(Current_Month, 9.0);
    /* ...and the rest of your code */
%mend transform_this;

Any reason you thought you can't?

Upvotes: 2

SAS2Python
SAS2Python

Reputation: 1297

keep June19_: Account_:;

This keeps all variables starting with June19_ and Account_ which are the ones you need evidently.

Upvotes: 3

Related Questions