BookmarkSubscribeRSS Feed
ravikiran2627
Calcite | Level 5

i have a dataset 

 

data xxx;

id gender

1      M

2      M

3      M

4      M

5      F

6      F

7      F

8      F

 

 

i need the output this way

 

id gender

1      M

2      F

3      M

4      F

5      M

6      F

7      M

8      F

 

how to do it in base sas.. can anyone hep me out in this pls

3 REPLIES 3
Astounding
PROC Star
Should ID really change just because the order of the observations changes?

Here is an approach that works but doesn't change ID. It requires that your data contains equal numbers of M and F values:

Data want;
Set have (where=(gender="M"));
output;
Set have (where=(gender="F"));
output;
run;

The ID can be recomputed if necessary. Instead of

1 2 3 4 5 6 7 8

you will get

1 5 2 6 3 7 4 8

It's not clear which set of ID values would be more useful. Perhaps there should even be a third variable added to the new data set so you would both the original order and the new order available.
Ksharp
Super User
 

data xxx;
input id gender $;
drop id;
cards;
1      M
2      M
3      M
4      M
5      F
6      F
7      F
8      F
;
data temp;
 set xxx;
 by gender notsorted;
 if first.gender then n=0;
 n+1;
run;
proc sort data=temp;
by n descending gender ;
run;
data want;
 set temp;
 id+1;
run;

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!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 3 replies
  • 650 views
  • 0 likes
  • 4 in conversation