BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
woo
Lapis Lazuli | Level 10 woo
Lapis Lazuli | Level 10

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
Lapis Lazuli | Level 10 woo
Lapis Lazuli | Level 10

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
Lapis Lazuli | Level 10 woo
Lapis Lazuli | Level 10

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
Lapis Lazuli | Level 10 woo
Lapis Lazuli | Level 10

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
Lapis Lazuli | Level 10 woo
Lapis Lazuli | Level 10

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

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

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.

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