Link to home
Start Free TrialLog in
Avatar of labradorchik
labradorchikFlag for United States of America

asked on

How to do variable modifications in a SAS dataset?

Hello, I am trying to perform variable modifications.

I am trying to recreate SAS work dataset Data1 by performing the following:
1. Use SAS work dataset Data1 as input.
2. Drop the following variables, if they exist: Var1 Var2 Var3
3. Rename Var5 to Var15.
4. Create the Var5 variable as NUM (type) and 8 (length)
5. Convert Var15 to its numeric equivalent and store in Var5.
6. Maintain SAS binary compression.

So, I am not sure on step #4 & #5. Please see my code in progress:

data Data1 (compress=binary);
  drop Var1 Var2 Var3;
 set dir.Data1;

Var5=Var15;
.....

run;

Open in new window


Any help will be greatly appreciated!
Avatar of Aloysius Low
Aloysius Low
Flag of Singapore image

data Data1 (
    drop = Var1 Var2 Var3 /*#2*/
    rename = (Var5 = Var15) /*#3*/
    compress=binary /*#6*/);
  set Data1; /*#1*/
  length Var5 8.; /*#4*/

  Var5=input(Var15, best.); /*#5*/
run;
Avatar of labradorchik

ASKER

Thank you very much, lowaloysius!
Quick question: do I need to check in the code if Var1, Var2, and Var3 are actually there in the dataset or I will not get any error messages if I just have those variables dropped as you written above?
Here are some example
data gems;
   input Name $ Color $ Carats Owner $;
   datalines;
emerald green 1 smith
sapphire blue 2 johnson
ruby red 1 clark
;

data lollipops;
   Flavor="Cherry";
   attrib Flavor format=$10.;
run;

data lollipops;
   Flavor="Cherry";
   attrib Flavor format=$10.;
run;

Open in new window

if the variable *may* not exist and you want to avoid an error message then use the drop statement instead in the code:

data Data1 (
    rename = (Var5 = Var15) /*#3*/
    compress=binary /*#6*/);
  set Data1; /*#1*/
  length Var5 8.; /*#4*/

  Var5=input(Var15, best.); /*#5*/
  drop Var1 Var2 Var3; /*#2*/ 
run;

Open in new window

SOLUTION
Avatar of Ian
Ian
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Lowaloysius and ShannonEE,
Thank you very much for your suggestions and explanations!

I was totally confused where to place that drop statement.
Thank you again!!