Kasia Jabłońska
Kasia Jabłońska

Reputation: 1

Adding a column is SAS using MODIFY (no sql)

I'm new to SAS and have some problems with adding a column to existing data set in SAS using MODIFY statement (without proc sql).

Let's say I have data like this

id name  salary  perks
1  John   2000      50  
2  Mary   3000     120

What I need to get is a new column with the sum of salary and perks. I tried to do it this way

data data1;
modify data1;
money=salary+perks;
run;

but apparently it doesn't work. I would be grateful for any help!

Upvotes: 0

Views: 1360

Answers (3)

Andi
Andi

Reputation: 1

If you want to use a data set you do it like this:

data dataset;
    set dataset;
 
    format new_column $12;
    new_column = 'xxx';

run;

Or use Proc SQL and ALTER TABLE.

proc sql;
    alter table dataset
    add new_column char(8) format = $12.
    ;
quit;

Upvotes: 0

Reeza
Reeza

Reputation: 21294

As @Tom mentioned you use SET to access the dataset.

I generally don't recommend programming this way with the same name in set and data statements, especially as you're learning SAS. This is because it's harder to detect errors, since once run and encounter an error, you destroy your original dataset and have to recreate it before you start again.

If you want to work step by step, consider intermediary datasets and then clean up after you're done by using proc datasets to delete any unnecessary intermediary datasets. Use a naming conventions to be able to drop them all at once, i.e. data1, data2, data3 can be referenced as data1-data3 or data:.

 data data2;
    set data1;
    money = salary + perks;
 run;

You do now have two datasets but it's easy to drop datasets later on and you can now run your code in sections rather than running all at once.

Here's how you would drop intermediary datasets

  proc datasets library=work nodetails holist;
  delete data1-data3;
  run;quit;

Upvotes: 2

Tom
Tom

Reputation: 51621

You can't add a column to an existing dataset. You can make a new dataset with the same name.

data data1;
  set data1;
  money=salary+perks;
run;

SAS will build it as a new physical file (with a temporary name) and when the step finishes without error it deletes the original and renames the new one.

Upvotes: 1

Related Questions