DATA Step, Macro, Functions and more

Macro replaceprefix: how to modify to pull labels and formats?

Accepted Solution Solved
Reply
New Contributor
Posts: 3
Accepted Solution

Macro replaceprefix: how to modify to pull labels and formats?

I have monthly data collected over two years. The datafiles have M# at the start of some of the variables, but not all of them (some have a different prefix, some have no prefix). I have successfully used the macro in this paper (http://support.sas.com/resources/papers/proceedings09/075-2009.pdf) to strip off the prefix using repeated runs of the macro to capture all of the variables that I need to have the prefix removed from. The problem is that this creates new variables that do not have the labels and formats that were assigned to the original variables. How can I modify this code to pull over the labels and formats as well?

Code from Weng and Fung paper:

%macro replaceprefix(dsn,dsnout,start,end,oldprefix,newprefix);

data temp;

set &dsn.;

run;

%LET ds=%SYSFUNC(OPEN(temp,i));

%let ol=%length(&oldprefix.);

%do i=&start %to &end;

%let dsvn&i=%SYSFUNC(VARNAME(&ds,&i));

%let l=%length(&&dsvn&i);

%let vn&i=&newprefix.%SUBSTR(&&dsvn&i,&ol+1,%EVAL(&l-&ol));

%end;

data &dsnout.;

set temp;

%do i=&start %to &end;

&&vn&i=&&dsvn&i;

drop &&dsvn&i;

%end;

%let rc=%SYSFUNC(CLOSE(&ds));

proc contents data=&dsnout. varnum;

title 'Replacing Prefix on Selected variables ';

run;

%mend replaceprefix;


Accepted Solutions
Solution
‎02-28-2014 05:18 PM
Super User
Posts: 17,886

Re: Macro replaceprefix: how to modify to pull labels and formats?

Why not just rename the variables rather than copy/drop?

data &dsnout.;

set temp;

%do i=&start %to &end;

rename &&dsvn&i=&&vn&i;

%end;

%let rc.....

View solution in original post


All Replies
Solution
‎02-28-2014 05:18 PM
Super User
Posts: 17,886

Re: Macro replaceprefix: how to modify to pull labels and formats?

Why not just rename the variables rather than copy/drop?

data &dsnout.;

set temp;

%do i=&start %to &end;

rename &&dsvn&i=&&vn&i;

%end;

%let rc.....

New Contributor
Posts: 3

Re: Macro replaceprefix: how to modify to pull labels and formats?

I had tried that, but now see the error was that I didn't remove the "drop &&dsvn&i;" line. Thanks so much!!

Valued Guide
Posts: 3,208

Re: Macro replaceprefix: how to modify to pull labels and formats?

The paper is describing a solution for a problem that is not yours.  As you understand the solution for the problem you can modify that.

Perhaps more easy to build your own. My analyses:

Step 1: Collect the needed information on variables

   The paper is using Proc SQL on the dictionary  -- macro rename

   Collecting the list in macro-array. 

      
Step 2: Execute changes

   The paper is using proc datasets ... modify

   As the the list is a macro array the list is processed in macro.

   Base SAS(R) 9.3 Procedures Guide, Second Edition  Proc datasets modify...

   You can use attrib to assing all attributes as well at the same maco process statements

   Needing some additional logic can be don with macro programming %if ... %then  handling the strings 

---->-- ja karman --<-----
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 234 views
  • 0 likes
  • 3 in conversation