Help using Base SAS procedures

How to use drop and rename in the data statement at one time

Reply
Regular Contributor
Posts: 194

How to use drop and rename in the data statement at one time

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!

PROC Star
Posts: 307

Re: How to use drop and rename in the data statement at one time

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;
Super User
Super User
Posts: 7,050

Re: How to use drop and rename in the data statement at one time

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;
Regular Contributor
Posts: 194

Re: How to use drop and rename in the data statement at one time

Thank you both!  Super helpful!!

Ask a Question
Discussion stats
  • 3 replies
  • 164 views
  • 2 likes
  • 3 in conversation