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;

 Capture.PNG

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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

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
  • 4 replies
  • 1724 views
  • 3 likes
  • 4 in conversation