BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
maroulator
Obsidian | Level 7

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

1 ACCEPTED SOLUTION

Accepted Solutions
stat_sas
Ammonite | Level 13

Try retain statement after adding variable to your existing dataset.

data want;

retain Name Sex  Age;

set have;

run;

View solution in original post

5 REPLIES 5
Jagadishkatam
Amethyst | Level 16

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

Thanks,
Jag
stat_sas
Ammonite | Level 13

Try retain statement after adding variable to your existing dataset.

data want;

retain Name Sex  Age;

set have;

run;

Hima
Obsidian | Level 7

What ever


maroulator
Obsidian | Level 7

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.

Hima
Obsidian | Level 7

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;

Capture.JPG

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
  • 5 replies
  • 2612 views
  • 7 likes
  • 4 in conversation