DATA Step, Macro, Functions and more

How to deal with multiple different column names in an effecient way

Accepted Solution Solved
Reply
Contributor
Posts: 48
Accepted Solution

How to deal with multiple different column names in an effecient way

I need to change something with all the variables in my dataset, but there are around 100 variables and at the same time, the column names are very different and irregular , for example, ws  tsfg  ttt  etttt ssdf  tkw  trp.

 

Of course I can't do the same behavior for each variable for 100 times. I am wondering is there an efficient way to do with the irregular variable names?


Accepted Solutions
Solution
‎07-18-2016 04:57 AM
Super User
Posts: 19,855

Re: How to deal with multiple different column names in an effecient way

If your recoding or doing some sort of calculation you can also use arrays and short references such as 

 

Array var_list(*) first_var -- last_var ;

 

this will include all variables from first to last based on variable portion. 

View solution in original post


All Replies
Super User
Posts: 19,855

Re: How to deal with multiple different column names in an effecient way

Yes ... But it depends on what you want to do. Common tasks are adding a prefix or suffix, renaming based on lookup table, or adding labels. 

 

Many of these can be automated by leveraging some basic macro variables and using sashelp.vcolumn/dictionary.column table. 

If you've never used the dictionary tables navigate to the sashelp library and take a look at .vtable vcolumn tables which have metadata about all your SAS datasets. 

 

 

Solution
‎07-18-2016 04:57 AM
Super User
Posts: 19,855

Re: How to deal with multiple different column names in an effecient way

If your recoding or doing some sort of calculation you can also use arrays and short references such as 

 

Array var_list(*) first_var -- last_var ;

 

this will include all variables from first to last based on variable portion. 

Respected Advisor
Posts: 4,930

Re: How to deal with multiple different column names in an effecient way

Assuming you have a list of name equivalents, transform that list into a macro variable which can be inserted in a rename statement:

 

data names;
length oldName newName $32;
input oldName newName;
datalines;
weight kilos
height inches
age years
name firstName
sex gender
;

proc sql;
select catx("=", oldName, newName) into : rename separated by " "
from names;
quit;

data myClass;
set sashelp.class;
rename &rename;
run;

proc contents data=myClass; run;
PG
☑ This topic is solved.

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

Discussion stats
  • 3 replies
  • 218 views
  • 2 likes
  • 3 in conversation