How to rename variables without using their original names?

I have a dataset that I upload to sas. There are always 4 variables in the same order. The problem is that sometimes variables can have slightly different names.

For example, the first variable user. The next day I get the same data set, it could be userid. Therefore, I can’t userename(user=my_user)

Is there any way to refer to a variable in their order., Something like this

rename(var_order_1=my_user) ;

rename(var_order_3=my_inc) ;

rename _ALL_=x1-x4 ;

+4
source share
2 answers

There are several ways to do this. One of them is to define variable names from PROC CONTENTSor dictionary.columnsand create rename commands.

data have;
input x1-x4;
datalines;
1 2 3 4
5 6 7 8
;;;;
run;


%macro rename(var=,newvar=); 
rename &var.=&newvar.;
%mend rename;

data my_vars;   *the list of your new variable names, and their variable number;
length varname $10;
input varnum varname $;
datalines;
1 FirstVar
2 SecondVar
3 ThirdVar
4 FourthVar
;;;;
run;

proc sql;     *Create a list of macro calls to the rename macro from joining dictionary.columns with your data. ;
              * Dictionary.columns is like proc contents.;
select cats('%rename(var=',name,',newvar=',varname,')')
  into :renamelist separated by ' '
  from dictionary.columns C, my_vars M
  where C.memname='HAVE' and C.libname='WORK'
    and C.varnum=M.varnum;
quit;

proc datasets;
modify have;
&renamelist;  *use the calls;
quit;

/ _INFILE_ ( ). . , , , .

data have;
input x1-x4;
datalines;
1 2 3 4
5 6 7 8
;;;;
run;

data want;
set have;
infile datalines truncover;   *or it will go to next line and EOF prematurely;
input @1 @@;                  *Reinitialize to the start of the line or it will eventually EOF early;
_infile_=catx(' ',of _all_);  *put to input stream as space delimited - if your data has spaces you need something else;
input y1-y4 @@;               *input as space delimited;
put _all_;                    *just checking our work, for debugging;
datalines;                    *dummy datalines (could use a dummy filename as well);

;;;;
run;
+4

.

data have;
   format var1-var4 $1.;
   call missing (of _all_);
run;
proc sql noprint;
select name into: namelist separated by ' '  /* create macro var */
   from dictionary.columns
   where libname='WORK' and memname='HAVE' /* uppercase */
   order by varnum; /* should be ordered by this anyway */

%macro create_rename(invar=);
%do x=1 %to %sysfunc(countw(&namelist,%str( )));
   /* OLDVAR = NEWVARx */
   %scan(&namelist,&x) = NEWVAR&x
%end;
%mend;

data want ;
   set have (rename=(%create_rename(invar=&namelist)));
   put _all_;
run;

:

NEWVAR1=  NEWVAR2=  NEWVAR3=  NEWVAR4=
+4

Source: https://habr.com/ru/post/1547979/


All Articles