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

Hi,

 

suppose that I have a table with 10 ids. Is it possible to create a new variable called "group" such that there are only say 3 groups and they are repetitive in the form 3,1,2,3,1,2,3,1,2 etc:

 

id group
1 3
2 1
3 2
4 3
5 1
6 2
7 3
8 1
9 2
10 3

 

Thank you!

 

1 ACCEPTED SOLUTION

Accepted Solutions
Shmuel
Garnet | Level 18

Use function mode() to create the group code:

 

data test;

     retain group 3; /* initial value, may be adapted to start with that value */

     do id=1 to 10;

          output;

          group = mod(group,3) +1;

     end;

run;

View solution in original post

11 REPLIES 11
Shmuel
Garnet | Level 18

Use function mode() to create the group code:

 

data test;

     retain group 3; /* initial value, may be adapted to start with that value */

     do id=1 to 10;

          output;

          group = mod(group,3) +1;

     end;

run;

LaurieF
Barite | Level 11

Umm - I think you mean 'mod'…

Shmuel
Garnet | Level 18
Thanks. You are right. it should be mod().
ilikesas
Barite | Level 11

Thanks for the code it works perfectly!

 

But suppsoe that instead of the groups 3,1,2 I had groups a,b,c - is it possible to have a more universal solution which can accomodate for any groups?

 

LaurieF
Barite | Level 11

Using the 'more than one way to skin a cat' process:

data test;
     array group_array{3} $ 1 _temporary_ ('a', 'b', 'c');
     retain groupn 2; /* initial value, may be adapted to start with that value */
     do id=1 to 10;
          groupn = mod(groupn, 3) + 1;
          group = group_array[groupn];
          output;
     end;
keep id group;
run;

Assigning the values to an array, then using the mod-ed groupn value, you get (in order) c / a / b / c…

Ksharp
Super User

data have;
array x{3} $ _temporary_ ('c' 'a' 'b');
 do id=1 to 10;
  if mod(id,3)=1 then n=0;
  n+1;
  group=x{n};
  output;
 end;
run;

Ksharp
Super User

data have;
array x{3} $ _temporary_ ('c' 'a' 'b');
 do id=1 to 10;
  if mod(id,3)=1 then n=0;
  n+1;
  group=x{n};
  output;
 end;
run;

ilikesas
Barite | Level 11

is it possible to assign a list such that:

 

array x{3} $ _temporary_ &list;

 

LaurieF
Barite | Level 11

Yes definitely. You have to be careful as to how you construct "&list", with the quoting especially, but it would be a great generalised solution.

ilikesas
Barite | Level 11

Thanks!

 

Strictly speaking, Shmuel's post is the answer to the question as I formulated it, but the other posts give immense insight into the general solution!

Ksharp
Super User

data have;
array x{3} $ _temporary_ ('c' 'a' 'b');
 do id=1 to 10;
  if mod(id,3)=1 then n=0;
  n+1;
  group=x{n};
  output;
 end;
run;

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
  • 11 replies
  • 1636 views
  • 8 likes
  • 4 in conversation