BookmarkSubscribeRSS Feed
thanikondharish
Fluorite | Level 6

 i have sashelp.class 

 

1 Alfred M 14 69.0 112.5
2 Alice F 13 56.5 84.0
3 Barbara F 13 65.3 98.0
4 Carol F 14 62.8 102.5
5 Henry M 14 63.5 102.5
6 James M 12 57.3 83.0
7 Jane F 12 59.8 84.5
8 Janet F 15 62.5 112.5
9 Jeffrey M 13 62.5 84.0
10 John M 12 59.0 99.5
11 Joyce F 11 51.3 50.5
12 Judy F 14 64.3 90.0
13 Louise F 12 56.3 77.0
14 Mary F 15 66.5 112.0
15 Philip M 16 72.0 150.0
16 Robert M 12 64.8 128.0 
17 Ronald M 15 67.0 133.0
18 Thomas M 11 57.5 85.0
19 William M 15 66.5 112.0

 

but to keep every 3rd record sex variable keep in samll letter like see below dataset

 

1 Alfred M 14 69.0 112.5
2 Alice F 13 56.5 84.0
3 Barbara F 13 65.3 98.0
4 Carol f 14 62.8 102.5
5 Henry m 14 63.5 102.5
6 James m 12 57.3 83.0
7 Jane F 12 59.8 84.5
8 Janet F 15 62.5 112.5
9 Jeffrey M 13 62.5 84.0
10 John m12 59.0 99.5
11 Joyce f 11 51.3 50.5
12 Judy f 14 64.3 90.0
13 Louise F 12 56.3 77.0
14 Mary F 15 66.5 112.0
15 Philip M 16 72.0 150.0
16 Robert m 12 64.8 128.0
17 Ronald m 15 67.0 133.0
18 Thomas m 11 57.5 85.0
19 William M 15 66.5 112.0

9 REPLIES 9
novinosrin
Tourmaline | Level 20
if mod(_n_,3)=0 then sex=lowcase(sex);
thanikondharish
Fluorite | Level 6
Not like that 1,2,3 observations capital
4,5,6 observations lowcase like that
novinosrin
Tourmaline | Level 20

Okay, got it

 


data want;
group+1;
do i=1 to 3 until(z);
set sashelp.class end=z;
if mod(group,2)=0 then sex=lowcase(sex);
output;
end;
drop group i;;
run;
JosvanderVelden
SAS Super FREQ
This should also work: if mod(ceil( _N_ / 3 ), 2) = 0 then sex = lowcase(sex);
FreelanceReinh
Jade | Level 19

Or

if mod(_n_+2,6)<3 then sex=lowcase(sex);

 

Edit:

@thanikondharish: Just to explain how to derive the IF condition systematically:

  1. Note that your pattern ("AAAaaaAAAaaa...") has a period of length 6.
    So, the 6 elements of the pattern in one period must correspond (one-to-one) to the values of mod(_n_,6):
    A A A a a a
    1 2 3 4 5 0
  2. This already gives you a first solution: mod(_n_,6) in (0,4,5).
  3. When you shift the pattern by two places to the right (remembering that the period "1 2 3 4 5 0" repeats as well), the lowercase letters correspond to 0, 1, 2 (rather than 4, 5, 0), which allows for shorter code: mod(_n_+2,6)❤️.
    Or you could shift it by one place to the left, leading to the equally short alternative solution mod(_n_-1,6)>2.
hashman
Ammonite | Level 13

@novinosrin :

You don't need GROUP; _N_ is automatically the group:

data want ;                                       
  do _iorc_ = 1 to 3 ;                            
    set sashelp.class ;                           
    if mod (_n_, 2) = 0 then sex = lowcase (sex) ;
    output ;                                      
  end ;                                           
run ;                                             

But I'd eagerly defer to the 1-liner by @JosvanderVelden:

data want ;                                                
  set sashelp.class ;                                      
  if not mod (ceil (_n_ / 3), 2) then sex = lowcase (sex) ;
run ;                                                      

since it needs no understanding of either the DoW-loop or the true behavior of _N_; just simple integer/modular arithmetic. 

 

Kind regards

Paul D.

novinosrin
Tourmaline | Level 20

Guru @hashman   Good morning, Sharp alert and it is proven now I lack ATD(Attention to detail) both on reading the question thoroughly, to the approach +plus a quantitative push. Hmm, really have to start working on it. Well, a cheeky excuse is I may have to go back to Dunkin and get a refund for my coffee or switch to starbucks. I blame the coffee for (ATD issues) 🙂

Tom
Super User Tom
Super User

Play around until you get a function that works.

data check;
  do n=1 to 10;
    div3 = int((n-1)/3);
    mod2 = mod(div3,2);
    output;
  end;
run;
proc print;
run;

Then use it with the _N_ automatic variable.

Let's use NAME instead of SEX to see the effect of both UPCASE() and LOWCASE().

data want;
  set sashelp.class ;
  if mod(int((_n_-1)/3),2) then name=lowcase(name);
  else name=upcase(name);
run;
proc print;
run;
Obs    Name       Sex    Age    Height    Weight

  1    ALFRED      M      14     69.0      112.5
  2    ALICE       F      13     56.5       84.0
  3    BARBARA     F      13     65.3       98.0
  4    carol       F      14     62.8      102.5
  5    henry       M      14     63.5      102.5
  6    james       M      12     57.3       83.0
  7    JANE        F      12     59.8       84.5
  8    JANET       F      15     62.5      112.5
  9    JEFFREY     M      13     62.5       84.0
 10    john        M      12     59.0       99.5
 11    joyce       F      11     51.3       50.5
 12    judy        F      14     64.3       90.0
 13    LOUISE      F      12     56.3       77.0
 14    MARY        F      15     66.5      112.0
 15    PHILIP      M      16     72.0      150.0
 16    robert      M      12     64.8      128.0
 17    ronald      M      15     67.0      133.0
 18    thomas      M      11     57.5       85.0
 19    WILLIAM     M      15     66.5      112.0

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Update

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
  • 9 replies
  • 1908 views
  • 5 likes
  • 6 in conversation