BookmarkSubscribeRSS Feed
sweettravel
Calcite | Level 5
Hi, all
I have the original data set like this:
Male Female V1 V2 V3 V4
3 2 2 4 5 7
2 3 6 9 3 8

Now I want to transform it into a new data set. Instead of male and female, now I have a new variable "gender", and gender=0 for male, gender=1 for female. If male=3, then repeat the observation 3 times with gender=0; if female=2, then repeat the observation 2 times with gender=1.

The transformed data set should look like:
Gender V1 V2 V3 V4
0 2 4 5 7
0 2 4 5 7
0 2 4 5 7
1 2 4 5 7
1 2 4 5 7
0 6 9 3 8
0 6 9 3 8
1 6 9 3 8
1 6 9 3 8
1 6 9 3 8


This task is really tricky for me. Wonder whether anyone have an idea of how to implement it. Thanks a lot!
2 REPLIES 2
Rambo
Calcite | Level 5
Hi Sweettravel,

You can use the output statement [along with a do loop] to help print duplicate records.

Try using the following code:

data transformed;
set original;

gender = 0; /*sets the gender to male*/
do m = 1 to male;
output transformed;
end;
gender = 1; /* sets the gender to female*/
do f = 1 to female;
output transformed;
end;
keep gender v1-v4; /*ensures the key variables are kept*/
run;


I will note that the code above assumes male and female are both greater than 0. If this is not the case, you will need to add a conditional logic check before dropping into the DO loops.

Rambo
sweettravel
Calcite | Level 5
Thanks a lot, Rambo. The code works great on my sample data, I will apply to my large data, too. Saves me lots of time, thanks again!

sas-innovate-2024.png

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.

 

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
  • 2 replies
  • 519 views
  • 0 likes
  • 2 in conversation