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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 3 replies
  • 473 views
  • 0 likes
  • 4 in conversation