BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Uddin
Fluorite | Level 6

Hi,

I have a data set where I want to edit observation. For example, the data set contains country names under 'country' variable like:

Country

Finland

Denmark

Norway

Iceland

Netherlands

Switzerland

Sweden

New Zeland

Canada

Austria

 

I want to edit "Switzerland" into "Swiss" and "Iceland" into "Nordic island" . I tried with replace statement but it is not working. Could you please help me regarding this. Looking for your kind response.

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Two ways: use a SELECT or a format in a data step.

1)

data want;
set have;
select (country);
  when ("Switzerland") country = "Swiss";
  when ("Iceland") country = "Nordic island";
  otherwise;
end;
run;

2)

proc format;
value $transcountry
  "Switzerland" = "Swiss"
  "Iceland" = "Nordic island"
;
run;

data want;
set have;
country = put(country,$transcountry.);
run;

View solution in original post

10 REPLIES 10
PaigeMiller
Diamond | Level 26

I tried with replace statement but it is not working. 

For your current and future benefit, never tell us something doesn't work and provide no other information. We don't know what you did, and we don't know why it isn't working. We can't help. But if you show us, we can help. Show us the entire LOG for this step, copy the entire log for this PROC or DATA step and paste it into the window that appears when you click on the </> icon.

2021-11-26 08_27_29-Reply to Message - SAS Support Communities — Mozilla Firefox.png

--
Paige Miller
Uddin
Fluorite | Level 6
Hi,

Sorry for that and thanks for your advice. Here is the Code I used:

data want;
set have;
Country="Swiss";
Replace var Country where Country="Switzerland";
run;

Looking for your kind response.

Kind Regards

Reeza
Super User

@Uddin wrote:
Hi,

Sorry for that and thanks for your advice. Here is the Code I used:

data want;
set have;
Country="Swiss";
Replace var Country where Country="Switzerland";
run;

Looking for your kind response.

Kind Regards


As far as I know that isn't valid SAS syntax. Are you using SAS Studio, CAS? VA?


EDIT: Looks like this is IML function, are you programming using SAS IML?

https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.3/imlug/imlug_langref_sect392.htm#:~:text=The%2....

 

Uddin
Fluorite | Level 6
Hi Reeza,
Thank you so much for your kind response. I am using SAS studio. Kind Regards
Tom
Super User Tom
Super User

To change the value of a variable just assign a new value.  Your code already includes an assignment statement, but it is unconditional so it will change COUNTRY to Swiss on every observation.

 

So just execute the assignment statement conditionally.

data want;
set have;
  if Country="Switzerland" then Country="Swiss";
run;

If you read the statement out loud it does what it says.

Uddin
Fluorite | Level 6
Hi Tom,

Thanks for your effort and advice. Yes, the code you provided is working. Brilliant!!!!!!
FreelanceReinh
Jade | Level 19

Hi @Uddin,

 

If you really want to use the REPLACE statement (i.e., edit dataset HAVE in place, not rewriting the unchanged observations), here's how it works:

data have;
modify have;
if country='Switzerland' then do;
  country='Swiss';
  replace;
end;
else if country='Iceland' then do;
  country='Nordic island';
  replace;
end;
run;

Or, alternatively, with only one REPLACE statement:

data have;
modify have;
w=whichc(country, 'Switzerland', 'Iceland');
if w then do;
  country=choosec(w, 'Swiss', 'Nordic island');
  replace;
end;
run;
Kurt_Bremser
Super User

@Uddin wrote:
Hi,

Sorry for that and thanks for your advice. Here is the Code I used:

data want;
set have;
Country="Swiss";
Replace var Country where Country="Switzerland";
run;

Looking for your kind response.

Kind Regards


Maxim 1: Read the Documentation.

REPLACE Statement 

You will immediately see that REPLACE is not the correct tool to use.

The most simple version is this:

if country = "Switzerland" then country = "Swiss";

But I have already shown two ways to do that for multiple values.

For more than a few values, store the original and replacement values in a dataset, from which you can either create the format, or which you can use in a join, or which you can use as a source for a hash object.

Kurt_Bremser
Super User

Two ways: use a SELECT or a format in a data step.

1)

data want;
set have;
select (country);
  when ("Switzerland") country = "Swiss";
  when ("Iceland") country = "Nordic island";
  otherwise;
end;
run;

2)

proc format;
value $transcountry
  "Switzerland" = "Swiss"
  "Iceland" = "Nordic island"
;
run;

data want;
set have;
country = put(country,$transcountry.);
run;
Uddin
Fluorite | Level 6
Hi Kurt,
Thank you so much for your kind support and advice. Yes, the code you provided is working. Amazing!!!!!!!Thanks

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 10 replies
  • 905 views
  • 0 likes
  • 6 in conversation