DATA Step, Macro, Functions and more

Replacing variable values with values with a space

Accepted Solution Solved
Reply
New Contributor
Posts: 4
Accepted Solution

Replacing variable values with values with a space

I need to merge two data sets, but one of them has one thing labelled two ways: "Place A" and "Place_A". I want to keep it as "Place A" Because I am plotting this data later on and the _ is not a beautiful thing. 

 

Then, I need to merge the data sets based on place, but that is not the issue here, I am trying to replace "Place_A" with "Place A" in one data set so when I merge, they are recognized as the same variable. Code:

 

data test2;

   set test;

   if PLACE = Place_A then PLACE = 'Place A'n;

run;

 

Error: "Place A is not a valid SAS name".

 

This is in SAS 9.2 Unix.

 

 


Accepted Solutions
Solution
‎08-03-2017 03:20 PM
Trusted Advisor
Posts: 1,586

Re: Replacing variable values with values with a space

Posted in reply to novinosrin

I assume you have a variable named PLACE in your input and you want to replace '_' with ' ' then

either do:

place = translate(place,' ','_'); /* arguments: variable name, replacement, to be replaced */

or correct your code using literals to distinguish from variables:

if place = 'Place_A'  then place = 'Place A';

in this case you can use either single quotes or double quotes.

View solution in original post


All Replies
Solution
‎08-03-2017 03:20 PM
Trusted Advisor
Posts: 1,586

Re: Replacing variable values with values with a space

Posted in reply to novinosrin

I assume you have a variable named PLACE in your input and you want to replace '_' with ' ' then

either do:

place = translate(place,' ','_'); /* arguments: variable name, replacement, to be replaced */

or correct your code using literals to distinguish from variables:

if place = 'Place_A'  then place = 'Place A';

in this case you can use either single quotes or double quotes.

New Contributor
Posts: 4

Re: Replacing variable values with values with a space

[ Edited ]

Just to clarify, I have a variable named PLACE and it has some of its values named Place_A that I want to change to Place A

New Contributor
Posts: 4

Re: Replacing variable values with values with a space

Posted in reply to novinosrin

Thank you, I added VALIDVARNAME = ANY in the data statement and it told me that it created a data set with 0 observations.

 

data test VALIDVARNAME = ANY;

  set test2;

  if PLACE = Place_A then PLACE = 'Place A'n;

run;

 

or: 

 

data test;

  set test2;

  VALIDVARNAME = ANY

  if PLACE = Place_A then PLACE = 'Place A'n;

run;

Trusted Advisor
Posts: 1,586

Re: Replacing variable values with values with a space

You are mixing variable name with variable value.

 

Is 'Place_A' a variable name ? If YES maybe you want to  add 

    LABEL Place_A = 'Place A';

then define you prefer use lables in the chart/report instead variable names.

 

Otherwise - see my previous post.

☑ This topic is solved.

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

Discussion stats
  • 5 replies
  • 127 views
  • 1 like
  • 3 in conversation