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
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
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
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
What is the purpose of this? And what if there were an unequal amount of M vs F?
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:
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:
No merge needed at all.
cynthia
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.