Hello friends,
I have existing dataset XYZ which has space in b/w variable name like below;
finance field
marketing strategy
goal to realign
process in place
now i am trying to using this dataset and wants to rename all above variables...how can i do that - please help.
i am using this logic but its not working.
data test;
set xyz;
rename
finance field=finance_field
marketing strategy=marketing_strategy
goal to realign=goal_to_realign
process in place=process_in_place
;
run;
Then post the code that doesn't work.
Encasing the variable with single quotes followed by a n in the rename statement should work.
Post the log from the following code Your original code modified:
data test;
set xyz;
rename
'finance field'n=finance_field
'marketing strategy'n=marketing_strategy
'goal to realign'n=goal_to_realign
'process in place'n=process_in_place
;
run;
1. Check that SAS didn't already do that for you and isn't showing you a label instead of name. Usually it does by default, though EG can be different.
2. Refer to variable with spaces as 'variable name'n
ie 'finance field'n=finance_field
i did try that way too but getting both variable into the final dataset;
finance field | finance_field
where "finance field" has value and "finance_field" doesn't have anything....
Also validvarname=any giving that WARNING MESSAGE
WARNING: Only Base procedures and SAS/STAT procedures have been tested for use with VALIDVARNAME=ANY. Other use of this option is considered experimental and may cause undetected errors.
Post a proc contents from your input dataset?
And/or code/log with any errors messages.
proc contents data=have; run;
I'm assuming that this data has been imported into SAS from an external source otherwise you wouldn't have variable names with spaces unless you explicitly coded for them like 'finance field'n.
If you re-run your import code with this option:
options validvarname = V7;
It will automatically rename your SAS variables to replace spaces with underscores.
proc contents data=have varnum ; run;
# variable type len format informat label
1 finance field char 16 finance field
2 marketing strategy char 25 marketing strategy
3 goal to realign char 20 goal to realign
4 process in place char 25 process in place
Dataset is already existed and i don't know from which source (row file) or i would reread with my own variable say using "_" in b/w
What was the encoding reported by proc contents?
In that case this works for me:
options validvarname = Any;
data test;
attrib 'Name with blank'n length = $10;
run;
data test2;
rename 'Name with blank'n = Name_with_blank;
set test;
run;
Encoding = wlatin1 Western (Windows)
Engine = V9
Indexes= 0
OBS length=432
Then post the code that doesn't work.
Encasing the variable with single quotes followed by a n in the rename statement should work.
Post the log from the following code Your original code modified:
data test;
set xyz;
rename
'finance field'n=finance_field
'marketing strategy'n=marketing_strategy
'goal to realign'n=goal_to_realign
'process in place'n=process_in_place
;
run;
/*This data step has worked for me - i think we don't need two separate data step for it*/
options validvarname = Any;
data test;
attrib 'Name with blank'n length = $10;
rename 'Name with blank'n = Name_with_blank;
set test;
run;
@ Reeza - your code ran fine too...
Thanks a lot all of you
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.