BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
BrahmanandaRao
Lapis Lazuli | Level 10
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 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

 

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.

View solution in original post

11 REPLIES 11
japelin
Rhodochrosite | Level 12

 

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.

 

BrahmanandaRao
Lapis Lazuli | Level 10

Hi kawakami

how to reorder after giving alias name for defined variales x ,y  in proc sql

ballardw
Super User

 

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.

ChrisNZ
Tourmaline | Level 20

This works too:

proc datasets library=work nodetails nolist;
 modify ds;
 rename x=ytemp
        y=xtemp
 ;
 rename ytemp=y
        xtemp=x
 ;
quit;
ballardw
Super User

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.

Astounding
PROC Star

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.)

ballardw
Super User

@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.
Ksharp
Super User
data ds;
X=10;
Y=9;
run;

data ds1;
set ds (rename=(X=_X Y=X _X=Y));
run;
ChrisNZ
Tourmaline | Level 20

rename x=tmp y=x tmp=y;
in proc datasets works too.
Rewriting the whole table is undesirable @Ksharp.

Ksharp
Super User
Sure . if it was a big table .
If it was a small table ,it doesn't matter whether use Data Step or Proc Datasets .
ChrisNZ
Tourmaline | Level 20

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.

SAS Innovate 2025: Register Now

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!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 11 replies
  • 2587 views
  • 5 likes
  • 6 in conversation