BookmarkSubscribeRSS Feed
jcis7
Pyrite | Level 9

Hi,

I'm trying to drop variables and rename variables in a dataset.

 

I have var1 and var2 in the orignal dataset and am converting them into percentages using arrays.  I'd like the variables in the output dataset to have the  same names var1 and var2 so I'm trying to rename the new variables I created (pctvar1 pctvar2) to become var1 and var2 and also drop the old var1 and var2 variables that I don't need. 

 

data round (drop=var1 var2)

rename= (pctvar1=var1 pctvar2=var2);

set first;

 

*create percentages;

array mult var1 var2;

array pct pctvar1 pctvar2;

do over mult

 pct=mult*100;

end;

 

*--round to nearest tenth;

do over pct;

 pct=round(pct, .1);

end;

run;

 

 

 

What am I doing wrong?  thanks!

6 REPLIES 6
collinelliot
Barite | Level 11

Ignore my stupid calculations, but it looks like you want to do the following, in which case it's just a matter of getting your parentheses correct:

 

data first;
    var1 = 1;
    var2 = 1;
run;

data round(drop = var1 var2 rename= (pctvar1=var1 pctvar2=var2));
    set first;
    pctVar1 = var1 / 10;
    pctVar2 = var2 / 10;
run;
Tom
Super User Tom
Super User

A little reformatting of your DATA statement will probably make the error more obvious.

data 
  round (drop=var1 var2)
  rename= (pctvar1=var1 pctvar2=var2)
;

You have one output dataset name ROUND with the DROP= dataset option.

 Then it looks like you are starting to also define another output dataset named RENAME, but you added an extra = sign after the data set name.

 

So fixing the nesting of your () so that you have just one output dataset you get this syntax:

data round 
 (drop=var1 var2
  rename= (pctvar1=var1 pctvar2=var2)
 )
;

It probably is easier to type if you just use the DROP and RENAME statements instead of adding dataset options to the output dataset.

data round ;
  set .... ;
  ....
  drop var1 var2;
  rename pctvar1=var1 pctvar2=var2;
run;
jcis7
Pyrite | Level 9

Thank you both!  Super helpful!!

vidyapedii
Fluorite | Level 6

data first;
var1 = 1;
var2 = 1;
var3= 3;
run;

data round (drop=var3 rename=(var1=pctvar1 var2=pctvar2));
set first ;
Var1 = var1 / 10;
Var2 = var2 / 10;
run;

 

you dont need drop the variable its been already renamed 

_maldini_
Barite | Level 11
What is the order of operations here? Doesn't this syntax drop variables that were renamed?
What syntax would allow you to drop the original variables after renaming them? In other words, pctvar1 gets renamed to var1, pctvar2 gets renamed to var2 and then pctvar1 and pctvar1 get dropped.
Tom
Super User Tom
Super User

Dataset options are processed in the alphabetical order:

Drop=/Keep= first

Rename= second

Where= last

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 6 replies
  • 17147 views
  • 10 likes
  • 5 in conversation