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;
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;
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;
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.
Not tested :
options validvarname=any;
data ex;
length 'DTH + RB'n $17.;
set A B;
run;
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!
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.
Ready to level-up your skills? Choose your own adventure.