BookmarkSubscribeRSS Feed
thanikondharish
Fluorite | Level 6
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
9 REPLIES 9
Kurt_Bremser
Super User

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.

PeterClemmensen
Tourmaline | Level 20

Do you want to create a new variable for every three records in sashelp.class?

 

And what does your final result look like?

thanikondharish
Fluorite | Level 6
Single data set and sashelp.class has 19 records so new variable should be
like this
OBS. Flag
1. 1-3
2. 1-3
...........
19. 19-21
Jagadishkatam
Amethyst | Level 16

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;
Thanks,
Jag
BrahmanandaRao
Lapis Lazuli | Level 10

What is the logic as you required i didn't get

 

 

 

Regards,

Anand

ballardw
Super User

@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??????

 

 

Tom
Super User Tom
Super User

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

 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

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!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 9 replies
  • 2416 views
  • 5 likes
  • 7 in conversation