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

Hi there.

 

I used the macro %macro replaceprefix(lib,dsn,start,end,oldprefix,newprefix); to perform the prefix addition on selected variables and it is accesible from http://support.sas.com/resources/papers/proceedings09/075-2009.pdf. However, it doesn't retain the format and label of the variables. 

 

Can I please ask your help to shed some lights on this? Thank you very much.

 

1 ACCEPTED SOLUTION

Accepted Solutions
Miracle
Barite | Level 11

Hi @ankit___gupta,

Please find modified SAS code below (last 2 %let statements within the first do loop, label and format statement in the second datastep).

HTH. Thank you.

 

/*
Paper 075-2009
Renaming in Batches
Vincent Weng, Educational Testing Service, Princeton, NJ
Ying Feng, CTB/McGraw-Hill, Monterey, CA 

accessible via http://support.sas.com/resources/papers/proceedings09/075-2009.pdf
*/

/* Replacing Prefix on Selected Variables */
%macro replaceprefix(lib,dsn,start,end,oldprefix,newprefix);
proc contents data=&lib..&dsn.; title 'before renaming'; run;
data temp; set &lib..&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));

	%LET dsvnlab&i=%BQUOTE(%SYSFUNC(VARLABEL(&ds,%SYSFUNC(varnum(&ds,&&dsvn&i)))));
	%LET dsvnfmt&i=%SYSFUNC(VARFMT(&ds,%SYSFUNC(varnum(&ds,&&dsvn&i))));

%end;
data &lib..&dsn.;
	set temp;

	%DO i=&start %TO &end; 
		&&vn&i=&&dsvn&i; 
		label &&vn&i=%STR(&&dsvnlab&i);
		format &&vn&i &&dsvnfmt&i.;
		drop &&dsvn&i; 
	%end;
	%let rc=%SYSFUNC(CLOSE(&ds));
	proc contents data=&lib..&dsn.;
	title 'Replacing Prefix on Selected variables ';
run;
%mend replaceprefix;

View solution in original post

5 REPLIES 5
ankit___gupta
Quartz | Level 8

Hi Wong,

 

Why don't you set the format and label once more after the renaming.

 

In the  %macro replaceprefix(lib,dsn,start,end,oldprefix,newprefix); try adding the following.

                          

        

DATA    <lib>.<datasetname>;
    SET <lib>.<datasetname>;
        FORMAT <variable name> <format>;
	INFORMAT  <variable name> <format>;
        LABEL <label u want to give>;
RUN;

 

 

Hope it helps.

Astounding
PROC Star

Shed some light?  Yes.  Fix?  No.

 

You'll have to inspect the details within the macro you referenced.  It uses a DATA step to copy the original variables to new variables (then subsequently drops the original variables).  When you do that, formats and labels do not get copied, just the data values.

 

There are other macros within the paper that don't do that, using PROC DATASETS instead of a DATA step.  But those macros don't perform the exact same function as the one you are using.

Miracle
Barite | Level 11

Problem's now solved after adding a few lines of SAS statements to the macro.

Thank you Smiley Happy

ankit___gupta
Quartz | Level 8

Hi wong ,

 

So what was the solution ? Can u share ?

 

 

Miracle
Barite | Level 11

Hi @ankit___gupta,

Please find modified SAS code below (last 2 %let statements within the first do loop, label and format statement in the second datastep).

HTH. Thank you.

 

/*
Paper 075-2009
Renaming in Batches
Vincent Weng, Educational Testing Service, Princeton, NJ
Ying Feng, CTB/McGraw-Hill, Monterey, CA 

accessible via http://support.sas.com/resources/papers/proceedings09/075-2009.pdf
*/

/* Replacing Prefix on Selected Variables */
%macro replaceprefix(lib,dsn,start,end,oldprefix,newprefix);
proc contents data=&lib..&dsn.; title 'before renaming'; run;
data temp; set &lib..&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));

	%LET dsvnlab&i=%BQUOTE(%SYSFUNC(VARLABEL(&ds,%SYSFUNC(varnum(&ds,&&dsvn&i)))));
	%LET dsvnfmt&i=%SYSFUNC(VARFMT(&ds,%SYSFUNC(varnum(&ds,&&dsvn&i))));

%end;
data &lib..&dsn.;
	set temp;

	%DO i=&start %TO &end; 
		&&vn&i=&&dsvn&i; 
		label &&vn&i=%STR(&&dsvnlab&i);
		format &&vn&i &&dsvnfmt&i.;
		drop &&dsvn&i; 
	%end;
	%let rc=%SYSFUNC(CLOSE(&ds));
	proc contents data=&lib..&dsn.;
	title 'Replacing Prefix on Selected variables ';
run;
%mend replaceprefix;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 5 replies
  • 2019 views
  • 0 likes
  • 3 in conversation