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
if mod(_n_,3)=0 then sex=lowcase(sex);
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;
Very nice!
Or
if mod(_n_+2,6)<3 then sex=lowcase(sex);
Edit:
@thanikondharish: Just to explain how to derive the IF condition systematically:
A A A a a a 1 2 3 4 5 0
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.
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) 🙂
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
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
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.