BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Nitish1003
Obsidian | Level 7

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 

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

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

7 REPLIES 7
novinosrin
Tourmaline | Level 20

 

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

Nitish1003
Obsidian | Level 7
Hi Naveen
Thanks for the help. Can you please let me know the logic how this code works.
Regards
Nitish
novinosrin
Tourmaline | Level 20

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

Nitish1003
Obsidian | Level 7
Thanks got your Point Naveen
PeterClemmensen
Tourmaline | Level 20

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

Cynthia_sas
SAS Super FREQ

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

Nitish1003
Obsidian | Level 7
Thanks for the help.
I am just trying to learn sas.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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