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

How to define length in data step for char variable where the variable name contains special character '+' ?

 

I want to concatenate two datasets (A and B). When i try doing, the values in DTH + RB variables gets truncated due to different length of the variable in two datasets. Hence, I am trying to define the length for variable DTH + RB while concatenating but because of the special character in the variable DTH + RB the below codes are not working. 

 

Can you please suggest how to handle this special characters which are in variable names?

 

eg: A dataset

ID DTH + RB

1  a

2  abc

3 jeremiahpaul

4 58

5 99

 

eg: B dataset

ID DTH + RB

1  ab

2  abcd

3 jerereremhihahdjh

4 58

5 99

 

When i try the below code, sas separates the DTH and RB as different column

 

data ex;

length DTH + RB $17.;

set A B;

run;

 

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Under the option VALIDVARNAME=ANY, you can use (almost) any characters in variable names, but you need to use so-called "name literals" to address those variables:

length 'DTH + RB'n $17.;

But it is preferred to rename such variables to valid SAS names like dth_plus_rb, as these are much easier to use in  further coding:

data ex;
length 'DTH + RB' $17.;
set A B;
rename 'DTH + RB'=dth_plus_rb;
run;

View solution in original post

4 REPLIES 4
Kurt_Bremser
Super User

Under the option VALIDVARNAME=ANY, you can use (almost) any characters in variable names, but you need to use so-called "name literals" to address those variables:

length 'DTH + RB'n $17.;

But it is preferred to rename such variables to valid SAS names like dth_plus_rb, as these are much easier to use in  further coding:

data ex;
length 'DTH + RB' $17.;
set A B;
rename 'DTH + RB'=dth_plus_rb;
run;
Jannet
Obsidian | Level 7
Thank you so much ...I really appreciate ur time
gamotte
Rhodochrosite | Level 12

Hello,

 

This is not a valid SAS variable name. SAS has an option to deal with non valid variables

names but the best solution would be to rename your column.

 

https://documentation.sas.com/?cdcId=pgmsascdc&cdcVersion=9.4_3.5&docsetId=lesysoptsref&docsetTarget...

 

Not tested :

options validvarname=any;

data ex;
    length 'DTH + RB'n $17.;
    set A B;
run;

 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 1388 views
  • 2 likes
  • 3 in conversation