data ds;
X=10;
Y=9;
run;
data ds1;
retain X Y ;
set ds (rename=(X=Y Y=X));
run;
Any other method to swap the values without using arrays
Maybe:
data ds; X=10; Y=9; run;
proc datasets library=work nodetails nolist; modify ds; rename x=ytemp y=xtemp ; modify ds; rename ytemp=y xtemp=x ; run; quit; proc print data=ds; run;
Proc datasets won't let you rename a variable to another variable in the data set so requires two Modify blocks.
Advantage with Proc Datasets is that there is no reading of record by record as a Set statement uses. So will execute faster with moderate sized data sets or larger. And doesn't create another data set if that really isn't desired.
proc sql;
create table ds1 as
select Y as X
, X as Y from ds;
quit;
Also, if you don't want to specify your own variables, I think you can do it using macro variables.
Hi kawakami
how to reorder after giving alias name for defined variales x ,y in proc sql
Maybe:
data ds; X=10; Y=9; run;
proc datasets library=work nodetails nolist; modify ds; rename x=ytemp y=xtemp ; modify ds; rename ytemp=y xtemp=x ; run; quit; proc print data=ds; run;
Proc datasets won't let you rename a variable to another variable in the data set so requires two Modify blocks.
Advantage with Proc Datasets is that there is no reading of record by record as a Set statement uses. So will execute faster with moderate sized data sets or larger. And doesn't create another data set if that really isn't desired.
This works too:
proc datasets library=work nodetails nolist;
modify ds;
rename x=ytemp
y=xtemp
;
rename ytemp=y
xtemp=x
;
quit;
Yep.
Style habits involved with making sure I can tell later explicitly which data set is getting modified just like I practically never allow use of the _last_ data set as the data for a proc.
Good approach. I would not be shocked if this actually worked:
proc datasets library=work nodetails nolist;
modify ds;
rename x=y y=x;
run;
Did you try it? (Sorry, I no longer have the software available to test it myself.)
@Astounding wrote:
Good approach. I would not be shocked if this actually worked:
proc datasets library=work nodetails nolist; modify ds; rename x=y y=x; run;
Did you try it? (Sorry, I no longer have the software available to test it myself.)
Yes I tried that first though something in the back of mind whispered "I don't think this will work." from other places where Proc Datasets seems a bit touchier than the data step for some things.
5 proc datasets library=work nodetails nolist; NOTE: Writing HTML Body file: sashtml.htm 6 modify ds; 7 rename x=y y=x; ERROR: Variable y already exists on file WORK.DS. ERROR: Variable x already exists on file WORK.DS. 8 run; NOTE: Statements not processed because of errors noted above.
data ds; X=10; Y=9; run; data ds1; set ds (rename=(X=_X Y=X _X=Y)); run;
rename x=tmp y=x tmp=y;
in proc datasets works too.
Rewriting the whole table is undesirable @Ksharp.
But you don't know if it was a large table. Plus rewriting the data presents potential downsides other than performance, such as losing indexes or sort order, resetting the create date, or changing OS attributes such as the owner.
As a matter of habit, data steps really should not be used just to change attributes.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.