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

data have1;

My data have 240 variables that are named sequentially VAR_1 to VAR_240. I want to change the name on the variables to read like this: VAR 001  VAR 002   .....VAR  069..... VAR125.......VAR  240. Below is how the data looks like and I import it using proc import.

 

Thank you

 

infile datalines dlm=',';
input id $ VAR_1 VAR_2 VAR_3 VAR_4;
datalines;
1,4.19855,5.74574,33.46678,6.85391
3,3.48004,6.69138,31.85662,11.73753
4,3.33851,5.74293,36.09064,10.9801
9,3.2966,8.15718,30.27008,7.62836
;

run; 
 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User
proc transpose data=have out=long;
by id;
var var:;
run;

data want;
set long;
sequence = input(scan(_name_,2,"_"),best.);
drop _name_;
rename col1=value;
run;

You will probably be much better off keeping the long structure, but if you absolutely want the unwieldy wide layout, you can do this:

data long2;
set want;
idvar = 'VAR_' !! put(sequence,z3.);
run;

proc transpose data=long2 out=want_wide (drop=_name_);
by id;
var value;
id idvar;
run;

View solution in original post

9 REPLIES 9
mkeintz
PROC Star

If you really want to change names to a structure like  VAR 001   (i.e. with an internal blank), you are committing yourself to using name literals to refer to those variables.

 

I.e to rename VAR_001  to VAR 001, you would need the expression

rename   VAR_001='VAR 001'n

and all subsequent references to that varible would require you to use  'VAR 001'n instead of the far more easily typed  VAR_001.

 

Doing a rename of all the variables in a compact form would be relatively easy.  But do you really want to risk forgetting an apostrophe?

 

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
sbxkoenk
SAS Super FREQ
PROC DATASETS LIBRARY=MYLIB NoList memtype=DATA;
 modify MyDataSet;
  rename 
VAR_1   = 'VAR 001'n
VAR_2   = 'VAR 002'n
...
VAR_69  = 'VAR 069'n
...

VAR_125 = 'VAR 125'n
...
VAR_240 = 'VAR 240'n ; run;
QUIT;
Job04
Quartz | Level 8

Yes I needed the change to be consistent with other datasets and documentation. 

PaigeMiller
Diamond | Level 26

May I ask why you want to do this? It seems to me that you are committing to doing a fair amount of work, for (in my opinion) no benefit.

--
Paige Miller
Job04
Quartz | Level 8

I agree. But some work involves biochemists and other branches of medical field. When they generate data usually they put alot of description. Example, the sample 's id is too long that contains location of hospitals-phase of trial-patient's number-buffer : all this together. I'm doing this as matter of convenience and communication between people. 

Reeza
Super User

@Job04 wrote:

I agree. But some work involves biochemists and other branches of medical field. When they generate data usually they put alot of description. Example, the sample 's id is too long that contains location of hospitals-phase of trial-patient's number-buffer : all this together. I'm doing this as matter of convenience and communication between people. 


I would recommend using labels instead. When you export the data you can specify that the labels be output which are display friendly. Then the variable names are easier to program. 

Kurt_Bremser
Super User
proc transpose data=have out=long;
by id;
var var:;
run;

data want;
set long;
sequence = input(scan(_name_,2,"_"),best.);
drop _name_;
rename col1=value;
run;

You will probably be much better off keeping the long structure, but if you absolutely want the unwieldy wide layout, you can do this:

data long2;
set want;
idvar = 'VAR_' !! put(sequence,z3.);
run;

proc transpose data=long2 out=want_wide (drop=_name_);
by id;
var value;
id idvar;
run;
Job04
Quartz | Level 8

Perfect. Thank you so much!

FreelanceReinh
Jade | Level 19

@Job04 wrote:

My data have 240 variables that are named sequentially VAR_1 to VAR_240. (...) I import it using proc import.

You can use variable lists in a RENAME= dataset option of the dataset specified in the OUT= option of PROC IMPORT:

out=want(rename=(var_1-var_240 = var001-var240))

The same works with 'var 001'n-'var 240'n, but I wouldn't recommend such names.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 9 replies
  • 1013 views
  • 1 like
  • 7 in conversation