Desktop productivity for business analysts and programmers

dealing with space in variable

Accepted Solution Solved
Reply
Super Contributor
Super Contributor
Posts: 318
Accepted Solution

dealing with space in variable

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;


Accepted Solutions
Solution
‎10-21-2014 07:22 PM
Grand Advisor
Posts: 17,405

Re: dealing with space in variable

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


All Replies
Grand Advisor
Posts: 17,405

Re: dealing with space in variable

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

Super Contributor
Super Contributor
Posts: 318

Re: dealing with space in variable

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.

Grand Advisor
Posts: 17,405

Re: dealing with space in variable

Post a proc contents from your input dataset?

And/or code/log with any errors messages.

proc contents data=have; run;

Respected Advisor
Posts: 3,065

Re: dealing with space in variable

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.

Super Contributor
Super Contributor
Posts: 318

Re: dealing with space in variable

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

Grand Advisor
Posts: 10,229

Re: dealing with space in variable

What was the encoding reported by proc contents?

Respected Advisor
Posts: 3,065

Re: dealing with space in variable

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;

Super Contributor
Super Contributor
Posts: 318

Re: dealing with space in variable

Encoding = wlatin1 Western (Windows)

Engine = V9

Indexes= 0

OBS length=432

Solution
‎10-21-2014 07:22 PM
Grand Advisor
Posts: 17,405

Re: dealing with space in variable

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;

Super Contributor
Super Contributor
Posts: 318

Re: dealing with space in variable

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

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 10 replies
  • 21128 views
  • 5 likes
  • 4 in conversation