To give you some hints:
This can be achieved rather easily.
You need one retained variable that contains your group string.
Every time mod(_n_,3) hits 1, you set this variable, and you can calculate the necessary parts of the string from the current _n_.
The final data step needs only 5 statements (including "data" and "run").
Looking forward to seeing your attempt.
Do you want to create a new variable for every three records in sashelp.class?
And what does your final result look like?
Please show us the code you already tried for your homework.
Hope this will help , check the want variable
data want;
set sashelp.class;
retain new3 new4 want;
if mod(_N_+2,3)=0 then new=1;
else new+1;
if new>1 then new2=.;
else new2=new;
if new2 ne . then new3=_n_;
if new2 ne . then new4=_n_+2;
want=catx('-',put(new3,best.),put(new4,best.));
drop new:;
run;
What is the logic as you required i didn't get
Regards,
Anand
@thanikondharish wrote:
Sashelp.class has 19 records .Now create one new variable like for every three records 1-3
Ex:
1-3
1-3
1-3
4-6
4-6
4-6
And since the 19th record is the only one in the last "every three records" what should the value look like??????
Looks like a simple arithmetic problem. If you want the last flag value to reflect the actual number of observations then use the NOBS= option on the SET statement.
data want;
set sashelp.class nobs=nobs;
length flag $10 ;
flag=catx('-',int((_n_-1)/3)*3+1,min(int((_n_-1)/3)*3+3,nobs));
run;
proc print ;
var name flag;
run;
Obs Name flag 1 Alfred 1-3 2 Alice 1-3 3 Barbara 1-3 4 Carol 4-6 5 Henry 4-6 6 James 4-6 7 Jane 7-9 8 Janet 7-9 9 Jeffrey 7-9 10 John 10-12 11 Joyce 10-12 12 Judy 10-12 13 Louise 13-15 14 Mary 13-15 15 Philip 13-15 16 Robert 16-18 17 Ronald 16-18 18 Thomas 16-18 19 William 19-19
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.