DATA Step, Macro, Functions and more

why statement "fcod=&i" in macro does not work? How to use "rename" statements in Macro?

Reply
Contributor
Posts: 22

why statement "fcod=&i" in macro does not work? How to use "rename" statements in Macro?

I wan to split a data set  with variables lpt1 -lpt9 and  generate a new varialbe in new data sets. Run the following code

%macro test;

%do i=1 %to 9;

data test&i;

keep lpd&i;

fcod=&i;

run;

%end;

%mend;

%test;

There is  no variable fcod in new data set. why?

Another question:

I wand to rename variables in data set test1-test9 and run the following code ;

%macro rename;

%do i=1 %to 9;

data test&i;

set test&i;

rename LPT&i=lpt;

run;

%end;

%mend;

%rename;

SAS gives the following error message

"WARNING: The variable lpt1 in the DROP, KEEP, or RENAME list has never been referenced"

Whate's the mistake i made?

Regular Contributor
Posts: 195

Re: why statement "fcod=&i" in macro does not work? How to use "rename" statements in Macro?

I think you have not mentioned SET Statement and used KEEP Statement...So SAS is giving you error like variables in KEEP statement has never been refrenced...and u can also rename the variables in the same %DO Loops...

%macro loop;

  %do i = 1 %to 9;

       data test&i.(keep = lpt fcod);

         set SPECIFY_DATASET_NAME(keep = lpt&i.); /* you missed it */

         fcod = &i.;

         rename lpt&i. = lpt;

       run;

  %end;

%mend;

%loop;

-Urvish

Super Contributor
Posts: 282

Re: why statement "fcod=&i" in macro does not work? How to use "rename" statements in Macro?

Hi,

If you really do not have an existing SAS data set that should have been used in a set statement as Urvish suggested, then try changing the keep statement to a length statement, e.g.:

%macro test;

  %do i=1 %to 9;

    data test&i;

      length lpd&i $ 8; /* remove the "$" if numeric */

      fcod=&i;

    run;

  %end;

%mend;

%test;

In your next data step you want to rename LPT&i variables, but you named them lpd&i in your first data step, they should both be the same if you meant to refer to the same variables.

Further, if you want to just rename them later to remove the value generated by the macro variable i, then you could just create the variable in your first data step without the macro variable,  which will remove the need for the second data step. E.g. you could use the following in your first data step:

length lpd $ 8; /* remove the "$" if numeric */

Regards,

Amir.

Ask a Question
Discussion stats
  • 2 replies
  • 162 views
  • 0 likes
  • 3 in conversation