convert column names to numbers

Reply
New Contributor
Posts: 3

convert column names to numbers

Good day! here is my data

 

data samp1;
input treeno nob b1-b3;

cards;

1 10 11 12

2 13 14 15

3 16 17 18

;

 

and it prints as

treeno b1 b2 b3

1         10 11 12

2         13 14 15

3         16 17 18

 

what i would want to do is replace b1 to b3 as just numbers 1 to 3. could you pls help me? thanks

Respected Advisor
Posts: 3,836

Re: convert column names to numbers

SAS variables must follow naming rules and one of these rules states that a SAS variable name must start with an underscore or letter.

http://support.sas.com/documentation/cdl/en/lrcon/62955/HTML/default/viewer.htm#a000998953.htm

 

You can assign labels to variables for printing. See below:

data sample1;
  input treeno b1-b3;
  label b1='1' b2='2' b3='3';
cards;
1 10 11 12
2 13 14 15
3 16 17 18
;
run;

proc print data=sample1 noobs label;
run;

 Capture.PNG

Frequent Contributor
Posts: 107

Re: convert column names to numbers

You prolly need to use String literal after setting


options validvarname=any;
data samp1;
input treeno nob b1-b3;
cards;
1 10 11 12
2 13 14 15
3 16 17 18
;
data want;
set samp1;
rename b1='1'n b2='2'n b3='3'n ;
run;

 

Regards,

Naveen Srinivasan

Frequent Contributor
Posts: 123

Re: convert column names to numbers

As Naveen says you need to use 'options validvarname=any' but you don't need the rename step. This will work just as well

 

options validvarname=any;
data samp1;
input treeno "1"n-"3"n;
cards;
1 10 11 12
2 13 14 15
3 16 17 18
;
run;

proc print data=samp1;
run;

I must say though that validvarname =any is usually used when handling data from an external source such as an RDBMS or Excel when normal SAS naming rules don't apply. If you start down the route of using this option you'll have to use it in every piece of code which references this file.

Respected Advisor
Posts: 3,836

Re: convert column names to numbers

@ChrisBrooks

"...you'll have to use it in every piece of code which references this file"

And that's why I've proposed to use labels and didn't even mention SAS name literals for variable names (they are to be avoided).

Ask a Question
Discussion stats
  • 4 replies
  • 161 views
  • 3 likes
  • 4 in conversation