- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hi kawakami
how to reorder after giving alias name for defined variales x ,y in proc sql
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
This works too:
proc datasets library=work nodetails nolist;
modify ds;
rename x=ytemp
y=xtemp
;
rename ytemp=y
xtemp=x
;
quit;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.)
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
data ds; X=10; Y=9; run; data ds1; set ds (rename=(X=_X Y=X _X=Y)); run;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
rename x=tmp y=x tmp=y;
in proc datasets works too.
Rewriting the whole table is undesirable @Ksharp.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
If it was a small table ,it doesn't matter whether use Data Step or Proc Datasets .
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.