DATA Step, Macro, Functions and more

Adding a new column based on a logic statement

Reply
Frequent Contributor
Posts: 77

Adding a new column based on a logic statement

Hi guys,

 

Very new to sas and trying to add an additonal column based on a logic statement. Used this code

 

data practice.new;
if ageyr <= 8 then young="Y";
else young="N";
run;

 

It ended up replacing my entire column with 2 columns of ageyr and another column called young. What exactly happened? How do i make sure that everything else is not changed?

 

Thanks!

PROC Star
Posts: 768

Re: Adding a new column based on a logic statement

[ Edited ]

What entire column are you reffering to? Smiley Happy And can you provide some sample data?

Frequent Contributor
Posts: 77

Re: Adding a new column based on a logic statement

ageyr is the column im referring to

 

i want to create a new column called young that is just binary for Y and N

Trusted Advisor
Posts: 1,586

Re: Adding a new column based on a logic statement

Is that what you want:

data new;
  set have;
      if ageyr <= 8 then young="Y";
      else young="N";
      drop ageyr;
run;
 
Contributor
Posts: 21

Re: Adding a new column based on a logic statement


byeh2017 wrote:

data practice.new;
if ageyr <= 8 then young="Y";
else young="N";
run;


data practice.new; - this statement creates a data set named 'new' in 'practice' library

if practice.new is your source data

u need to define practice.new in the SET statement so that SAS fetches data from it

 

Super User
Posts: 5,518

Re: Adding a new column based on a logic statement

Let me repeat the most important comment you received so far.

 

No SET statement = no incoming data

 

So you created PRACTICE.NEW, but based upon no incoming data (based upon only your logic statements).  If you intended to make changes to PRACTICE.NEW, it is gone.  You would need to re-create it.

Frequent Contributor
Posts: 77

Re: Adding a new column based on a logic statement

Posted in reply to Astounding

Thanks. 

 

If i wanted to only modify the dataset I'm referring to. Do I just do this?

 

data practice.new1;
set practice.new1;
if ageyr <= 8 then young="Y";
else young="N";
run;

 

Trusted Advisor
Posts: 1,586

Re: Adding a new column based on a logic statement


byeh2017 wrote:

Thanks. 

 

If i wanted to only modify the dataset I'm referring to. Do I just do this?

 

data practice.new1;
set practice.new1;
if ageyr <= 8 then young="Y";
else young="N";
run;

 


Yes, this code will add new variable YOUNG to the dataset, with value based on AGEYR.

 

 

 

Super User
Posts: 5,518

Re: Adding a new column based on a logic statement

That would work.  However, note that by running the program without the SET statement, you have already wiped out your original data set.  You still need to re-create it first.

Super User
Posts: 11,343

Re: Adding a new column based on a logic statement


byeh2017 wrote:

Thanks. 

 

If i wanted to only modify the dataset I'm referring to. Do I just do this?

 

data practice.new1;
set practice.new1;
if ageyr <= 8 then young="Y";
else young="N";
run;

 


Yes this should work but until you become much more comfortable with SAS I would recommend creating new data sets as you do not actuall "add" the variable but recreate the entire dataset with a new variable added. The difference may look subtle but you have already likely destroyed an existing data set once and you may do so again with the same data set name on the Set and Data lines.

 

I have seen code where people were recoding values with something like:

if x<3 then x=x-1;

using the same input/output. They added a new variable and ran the code again. Added another code change. And then tried to figure out why they had negative values and zeroes they didn't expect.

Trusted Advisor
Posts: 1,586

Re: Adding a new column based on a logic statement

@byeh2017, pay attention, the meaning of each line in next skilton code:

 

DATA  libref.dsname;    <<< defines output dataset to a library given by libref, dataset name given as dsname.

  SET  libref.dsname;    <<< defines  input  dataset  from a library given by libref, dataset name given as dsname.

           ... any sas code need to produce output from input ...

RUN;                               <<< closes the data step ready to check syntax and execute .

 

DATA and SET may be assigned to different datasets, either differ by libref or by dsname or by both.

In case they are both assigned to same libref and dsname, sas will create a new dataset and replace the original.

 

There are also other ways to define input, beside of SET statement, like INFILE to read

external (not sas) file as xxx.txt or xxx.csv or DATALINES.

Anyhow, usually you need INPUT to create OUTPUT.

Ask a Question
Discussion stats
  • 10 replies
  • 263 views
  • 8 likes
  • 6 in conversation