BookmarkSubscribeRSS Feed
ShufeGuoding
Obsidian | Level 7

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?

2 REPLIES 2
UrvishShah
Fluorite | Level 6

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

Amir
PROC Star

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.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 681 views
  • 0 likes
  • 3 in conversation