BookmarkSubscribeRSS Feed
R_Win
Calcite | Level 5
i have a dataset it has two variables now i want to insert manually another varible to it how can i insert it.i have tryed set and merge also.
3 REPLIES 3
Cynthia_sas
SAS Super FREQ
Hi:
Consider the dataset SASHELP.CLASS...it has a set of variables. If I want to add a new variable to make a new version of the CLASS dataset, I have to:
1) Use a DATA step program (or a PROC SQL program)
2) Read in the existing dataset (with a SET statement or a FROM, if SQL)
3) Create my new variable with an assignment statement

For example:
[pre]
data work.newclass;
set sashelp.class;
newage = age + 5;
run;

proc print data=work.newclass;
run;
[/pre]

In this program, my "created" variable is called NEWAGE....it is based on adding 5 to the student's current age to find out how old the student will be in 5 years.

The input dataset is SASHELP.CLASS and the output dataset, WORK.NEWCLASS, will be an exact copy of the original dataset, but with a new variable. After you are sure the new version of the data is correct and is the way you want, then you could rename or replace the original dataset with the new dataset. I don't generally recommend that you replace your original data with changed data until after you are sure that all your program logic worked correctly.

That is the beauty of having WORK files. You can experiment and code and try things without losing your original file. Then, once things are working correctly, you can use utility programs to perform any housekeeping or renaming that you might want to do.

As always, backup early, backup often.

cynthia
R_Win
Calcite | Level 5
Thqs for your
Reply
but i want a varaible name as 'Employes' and the observations to be added are vijay,nani,kumar like that
Cynthia_sas
SAS Super FREQ
Ah, I see, you want to add observations, not just create new variables in an existing data set. There are several ways to do it. One would be to read the "starting" data from an input file and another way would be to have a data step program with multiple output statements.

for example[pre]
data empfile;
length employee $16;
employee='vijay';
age=12;
output;
employee='kumar';
age=14;
output;
employee='nani';
age=15;
output;
employee='stephanie';
age=16;
output;
run;

proc print data=empfile;
title 'empfile';
run;
[/pre]

In the above program, WORK.EMPFILE is the dataset being created. There are 2 variables: EMPLOYEE and AGE. Four observations are being created (4 OUTPUT statements). The program starts with the keyword "DATA" and ends with the keyword "RUN"; The program statements will execute one time, but every OUTPUT statement creates an observation to be written to the output dataset.

Alternately, I could read the data from "inline" data lines, like this:
[pre]

data empfile2;
length employee $16;
infile datalines;
input employee $ age;
return;
datalines;
vijay 12
kumar 14
nani 15
stephanie 16
;
run;

proc print data=empfile2;
title 'From "inline" data';
run;
[/pre]

In this second program, the program statements start with the keyword DATA and end with the keyword RETURN. The data that will become the observations comes after the DATALINES statement. SAS will loop through the DATA step statements one time for every line of data. There's no explicit OUTPUT statement in this program because there is an implied output at the "bottom" of every DATA step program.

If the data lines were stored in a file on disk, c:\temp\mydata.txt, then the above program could be written as:
[pre]
data empfile3;
length employee $16;
infile 'c:\temp\mydata.txt';
input employee $ age;
run;

proc print data=empfile3;
title 'From data on disk';
run;

[/pre]

There are many different forms that the INPUT statement could follow, depending on how the data lines are stored in the disk file. You could even read delimited data, CSV data and/or Excel files or other types of files, with using different options and statements.

The documentation on the subject of creating a SAS dataset by reading data "into" SAS format is quite thorough and contains lots of good examples. You just need to find the example that most closely resembles your data and then read about the statements used in those sample programs.

cynthia

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 936 views
  • 0 likes
  • 2 in conversation