BookmarkSubscribeRSS Feed
byeh2017
Quartz | Level 8

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!

10 REPLIES 10
PeterClemmensen
Tourmaline | Level 20

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

byeh2017
Quartz | Level 8

ageyr is the column im referring to

 

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

Shmuel
Garnet | Level 18

Is that what you want:

data new;
  set have;
      if ageyr <= 8 then young="Y";
      else young="N";
      drop ageyr;
run;
 
gauthamk28
Obsidian | Level 7

@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

 

Astounding
PROC Star

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.

byeh2017
Quartz | Level 8

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;

 

Shmuel
Garnet | Level 18

@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.

 

 

 

Astounding
PROC Star

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.

ballardw
Super User

@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.

Shmuel
Garnet | Level 18

@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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 10 replies
  • 32085 views
  • 8 likes
  • 6 in conversation