Does anyone know if there is a way (ie SAS command) to insert a new variable between existing SAS variables in a dataset?
For example I have the following dataset......
Name Age
Tom 10
John 18
Jill 23
Chris 25
Don 31
......and I want to end up with the dataset below.
Name Sex Age
Tom M 10
John M 18
Jill F 23
Chris M 25
Don M 31
Try retain statement after adding variable to your existing dataset.
data want;
retain Name Sex Age;
set have;
run;
if you have another dataset with name and sex variables , then you can use the merge statement to combine the age dataset with gender dataset by name variable and you can get the three variables in a single dataset.
something like
data want;
merge age gender;
by name;
run;
Thanks,
Jag
Try retain statement after adding variable to your existing dataset.
data want;
retain Name Sex Age;
set have;
run;
What ever Jagadishkatam and stat@sas provided works but for my understanding is the variable Sex on another table. If so, what is primary key and foriegn key relationship between those tables. Can you elobarate?
Hima,
There is no relationship per se. I have a dataset that already contains the Name and Age variables and I am trying to create/generate a new variable (Sex) between the two existing ones. I was hoping that there is a way to do this in SAS, similar to how you can insert a new column in Excel and fill it in with values.
I know how to create a new variable at the end of the variables in my existing dataset, but I was hoping to be able to create it betweent the two existing ones.
Thank you so much. I created a scenario to myself as per your explanation. Please adjust it your needs. What stat@sas provided works great in the situation.
DATA HAVE;
INPUT NAME $ AGE;
DATALINES;
TOM 10
JOHN 18
JILL 23
CHRIS 25
DON 31
;
RUN;
DATA WANT;
RETAIN NAME SEX AGE;
SET HAVE;
IF INDEX(NAME,'O') THEN
SEX='M';
ELSE SEX='F';
RUN;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.