SAS Enterprise Guide

Desktop productivity for business analysts and programmers
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
woo
Barite | Level 11 woo
Barite | Level 11

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;

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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;

View solution in original post

10 REPLIES 10
Reeza
Super User

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

woo
Barite | Level 11 woo
Barite | Level 11

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.

Reeza
Super User

Post a proc contents from your input dataset?

And/or code/log with any errors messages.

proc contents data=have; run;

SASKiwi
PROC Star

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.

woo
Barite | Level 11 woo
Barite | Level 11

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

ballardw
Super User

What was the encoding reported by proc contents?

SASKiwi
PROC Star

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;

woo
Barite | Level 11 woo
Barite | Level 11

Encoding = wlatin1 Western (Windows)

Engine = V9

Indexes= 0

OBS length=432

Reeza
Super User

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;

woo
Barite | Level 11 woo
Barite | Level 11

/*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

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

Register now!

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

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
  • 10 replies
  • 128668 views
  • 15 likes
  • 4 in conversation