DATA Step, Macro, Functions and more

Alternate gender in output

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 16
Accepted Solution

Alternate gender in output

Need a help to generate alternate gender output.

Dataset :-

data new;
input id gender $;
datalines ;
1 M
2 M
3 M
4 F
5 F
6 F
7 M
8 F
;
RUN;

 

Output :-

1 M
4 F
2 M
5 F
3 M
6 F
7 M
8 F

Please let me know how can i achieve it

Regards

Nitish 


Accepted Solutions
Solution
‎04-22-2017 08:43 AM
PROC Star
Posts: 169

Re: Alternate gender in output

I am not great at explaining as i am not smart. But here you go,

 

i. split the two into 2 datasets and apply filters on M and F to get them in sequence.

ii. Now you have both in the PDV

iii. In order, to avoid overwriting the values with common names, rename them before taking them to PDV

iv. Reset the values in the left table with the values in the right table on the 2nd explicit output after you have written the 1st explicit output.

 

HTH,

Naveen Srinivasan

View solution in original post


All Replies
PROC Star
Posts: 169

Re: Alternate gender in output

 

data want;

merge new(where=(gender='M')) new(rename=(id=_id gender=_gender) where=(_gender='F'));

output;

gender=_gender ;

id=_id;

output;

drop _:;

run;

 

Regards,

Naveen Srinivasan

Occasional Contributor
Posts: 16

Re: Alternate gender in output

Hi Naveen
Thanks for the help. Can you please let me know the logic how this code works.
Regards
Nitish
Solution
‎04-22-2017 08:43 AM
PROC Star
Posts: 169

Re: Alternate gender in output

I am not great at explaining as i am not smart. But here you go,

 

i. split the two into 2 datasets and apply filters on M and F to get them in sequence.

ii. Now you have both in the PDV

iii. In order, to avoid overwriting the values with common names, rename them before taking them to PDV

iv. Reset the values in the left table with the values in the right table on the 2nd explicit output after you have written the 1st explicit output.

 

HTH,

Naveen Srinivasan

Occasional Contributor
Posts: 16

Re: Alternate gender in output

Thanks got your Point Naveen
PROC Star
Posts: 551

Re: Alternate gender in output

What is the purpose of this? And what if there were an unequal amount of M vs F?

SAS Super FREQ
Posts: 8,743

Re: Alternate gender in output

[ Edited ]

Hi:

  I'm still not sure what the purpose is of doing this, but you are right that with an uneven number of males and females the merge approach might not work as desired.

 

  However, there is a much simpler approach that takes care of the uneven number of males and females and does not require a merge. Basically, you create a "helper" variable based on the gender where you are always incrementing a counter for males by 1 and incrementing the counter for females by 1.01. Then, you make a new counter to be the new order variable and sort by that order variable.

 

  For example, here's the code to read the data which is also making the helper variables, MORD, FORD and NEWORD:

data new;
input id gender $;
origord = _n_;
if gender = 'M' then do; 
  mord+1; 
  neword = mord; 
end;
else if gender='F' then do; 
  ford+1.01; 
  neword=ford; 
end;
datalines ;
1 M
2 M
3 M
4 F
5 F
6 F
7 M
8 F
9 F
10 M
11 F
;
RUN;

then, after this step, you have this:

before_sort.png

Note that I added some extra rows with an uequal number of males and females to test the scenario.

 

Then, all you have to do is sort by the NEWORD variable:

after_sort.png

 

No merge needed at all.

 

cynthia

Occasional Contributor
Posts: 16

Re: Alternate gender in output

Thanks for the help.
I am just trying to learn sas.
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 165 views
  • 3 likes
  • 4 in conversation