SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
cdvdc
Calcite | Level 5

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

4 REPLIES 4
Patrick
Opal | Level 21

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;

 undefined

novinosrin
Tourmaline | Level 20

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

ChrisBrooks
Ammonite | Level 13

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.

Patrick
Opal | Level 21

@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).

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

Register now!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 2645 views
  • 3 likes
  • 4 in conversation