I want to replace single observations in a dataset.
I have a var whose obs I want to change. Fir example each time my var =10, I want to replace it with 15 or another number or my choosing. How do I do this?'
I used to be able to do this with modify and replace statements, but have forgotten how to do it.
I prefer the SQL approach.
proc sql;
update have
set x1=15
where x1=10;
quit;
One advantage is it will work if the data in not in a SAS dataset.
data want;
set have;
if x1=10 then x1=15;
run;
"Observation" in SAS-speak is an entire "row of data" , "record" or similar. You want to change the value of a variable in specific observations, not "replace an observation".
In the poster's defense, I they were thinking of the modify/replace syntax in which you must replace the entire record to change a single value.
I prefer the SQL approach.
proc sql;
update have
set x1=15
where x1=10;
quit;
One advantage is it will work if the data in not in a SAS dataset.
If you really want to use the modify/replace style, my old paper should help. https://www.lexjansen.com/pnwsug/2008/CurtisMack-Modify.pdf . It is overkill for this case though.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and save with the early bird rate—just $795!
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.
Ready to level-up your skills? Choose your own adventure.