BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
blackpebble
Calcite | Level 5

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;

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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

3 REPLIES 3
Reeza
Super User

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.....

blackpebble
Calcite | Level 5

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

jakarman
Barite | Level 11

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 --<-----

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 3 replies
  • 861 views
  • 0 likes
  • 3 in conversation