DATA Step, Macro, Functions and more

Appending value to end of multiple variable names

Reply
N/A
Posts: 0

Appending value to end of multiple variable names

I have created a job that will compare each variable between two different files (of the same format).

My problem right now is that I need to identify which file the data came from so that others looking at the reports can make sense of it. For instance,

File has the following fields:
INTN
FIRST
LAST
BIRTH_DT

I was thinking a simple way to distinguish where the value came from would be to have them called:
INTN_NEW or INTN_OLD
FIRST_NEW or INTN_OLD
LAST_NEW or LAST_OLD
BIRTH_DT_NEW or BIRTH_DT_OLD.

The variable names will change, depending on the files being read in so I can not hard code the variables and their new names. I was thinking a macro might work but then realized that it isn't really a repetitive action since I would be renaming all of the variables for every row at once. I have no idea where to start.

I tried searching these forums for past posts that might help (searched for RENAME, RENAMING, APPEND, ALTER VAR, and none of them had posts which really helped point me in a solid direction).
Super User
Posts: 5,431

Re: Appending value to end of multiple variable names

Posted in reply to deleted_user
I guess that you could use some macro programing by using information from DICTIONARY.COLUMNS so you could make your code dynamic. Have a look at proc sql select :into clause, and then use the result with rename data set option.

Good luck!

Linus
Data never sleeps
Super Contributor
Posts: 474

Re: Appending value to end of multiple variable names

Posted in reply to deleted_user
Hi.

Yes, DICTIONARY.COLUMNS or PROC CONTENTS with output to a dataset.

Next, you just need to load some macro variables with the input of the DICTIONARY.COLUMS/output from PROC CONTENTS and sufix them with _NEW / _OLD.

For examples:

proc contents data = WORK.INFILE out = WORK._CONTENTS noprint;
run;

* cats is just use for text trimming;
data _null_;
set WORK._CONTENTS end = _EOF; /* _EOF = 1 when end of file */

/* create VARX_NEW and VARX_OLD macro vars */
/* _N_ is the current observation number, NAME is the variable name */
call symput(cats('VAR',put(_N_,best.),'_NEW'),cats(NAME,'_NEW'));
call symput(cats('VAR',put(_N_,best.),'_OLD'),cats(NAME,'_OLD'));

/* if end of file store VAR_COUNT as total number of variables */
if _EOF then call symput('VAR_COUNT',cats(put(_N_,best.)));

run;

then you will be able to use in your code:

&VAR1_NEW
&VAR1_OLD
...

being &VAR_COUNT the total variable count.

Hope it helps.

Greetings from Portugal.

Daniel Santos at www.cgd.pt
N/A
Posts: 0

Re: Appending value to end of multiple variable names

Posted in reply to DanielSantos
Daniel,

That worked like a charm! Once I used that...I simply had to rename the variables like this...

PROC DATASETS;
MODIFY WORK.INFFILE;

RENAME VAR1 = &VAR1_NEW;
RENAME VAR2 = &VAR2_NEW;
RENAME VAR3 = &VAR3_NEW;

RUN;
Respected Advisor
Posts: 3,799

Re: Appending value to end of multiple variable names

Posted in reply to deleted_user
> I have created a job that will compare each variable
> between two different files (of the same format).

That sounds like PROC COMPARE. No need to fiddle changing anything.

If you only want to COMPARE variable names, attributes, and location use OBS=0;
Ask a Question
Discussion stats
  • 4 replies
  • 172 views
  • 0 likes
  • 4 in conversation