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.
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;
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.
@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?
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.
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;
@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.
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.
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;
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.