BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
abak
Obsidian | Level 7

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.

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Shmuel
Garnet | Level 18

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

5 REPLIES 5
Shmuel
Garnet | Level 18

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.

abak
Obsidian | Level 7

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

abak
Obsidian | Level 7

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;

Shmuel
Garnet | Level 18

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.

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!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 5 replies
  • 828 views
  • 1 like
  • 3 in conversation