☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Quartz | Level 8

group wise sequence

``````data _null_;
set sashelp.class nobs=n;
where sex='M' ;
do i= 1 to 5 ;
put @5 name @20 sex  @28 i;
output;
end;
run;``````

Hi Experts,

I am trying below output same seq for each name with sex='M' records

required output

 Name Sex Group_seq Alfred M 1 Alfred M 1 Alfred M 1 Alfred M 1 Alfred M 1 Henry M 2 Henry M 2 Henry M 2 Henry M 2 Henry M 2 James M 1 James M 3 James M 3 James M 3 James M 3 Jeffrey M 4 Jeffrey M 4 Jeffrey M 4 Jeffrey M 4 Jeffrey M 4 John M 5 John M 5 John M 5 John M 5 John M 5 Philip M 6 Philip M 6 Philip M 6 Philip M 6 Philip M 6
1 ACCEPTED SOLUTION

Accepted Solutions
Super User

Re: group wise sequence

You are going to have a data set name on the DATA statement. DATA _NULL_ means there is not place for the OUTPUT statement to write to if you want a data set.

Something like this perhaps?

```data junk (keep= name sex Group_seq);
set sashelp.class nobs=n;
where sex='M' ;
by name;
retain group_seq;
if first.name then group_seq+1;
do i= 1 to 5 ;
output;
end;
run;```

I think your first row for James has the wrong group_seq value...

@pavank wrote:
``````data _null_;
set sashelp.class nobs=n;
where sex='M' ;
do i= 1 to 5 ;
put @5 name @20 sex  @28 i;
output;
end;
run;``````

Hi Experts,

I am trying below output same seq for each name with sex='M' records

required output

 Name Sex Group_seq Alfred M 1 Alfred M 1 Alfred M 1 Alfred M 1 Alfred M 1 Henry M 2 Henry M 2 Henry M 2 Henry M 2 Henry M 2 James M 1 James M 3 James M 3 James M 3 James M 3 Jeffrey M 4 Jeffrey M 4 Jeffrey M 4 Jeffrey M 4 Jeffrey M 4 John M 5 John M 5 John M 5 John M 5 John M 5 Philip M 6 Philip M 6 Philip M 6 Philip M 6 Philip M 6

4 REPLIES 4
Super User

Re: group wise sequence

You are going to have a data set name on the DATA statement. DATA _NULL_ means there is not place for the OUTPUT statement to write to if you want a data set.

Something like this perhaps?

```data junk (keep= name sex Group_seq);
set sashelp.class nobs=n;
where sex='M' ;
by name;
retain group_seq;
if first.name then group_seq+1;
do i= 1 to 5 ;
output;
end;
run;```

I think your first row for James has the wrong group_seq value...

@pavank wrote:
``````data _null_;
set sashelp.class nobs=n;
where sex='M' ;
do i= 1 to 5 ;
put @5 name @20 sex  @28 i;
output;
end;
run;``````

Hi Experts,

I am trying below output same seq for each name with sex='M' records

required output

 Name Sex Group_seq Alfred M 1 Alfred M 1 Alfred M 1 Alfred M 1 Alfred M 1 Henry M 2 Henry M 2 Henry M 2 Henry M 2 Henry M 2 James M 1 James M 3 James M 3 James M 3 James M 3 Jeffrey M 4 Jeffrey M 4 Jeffrey M 4 Jeffrey M 4 Jeffrey M 4 John M 5 John M 5 John M 5 John M 5 John M 5 Philip M 6 Philip M 6 Philip M 6 Philip M 6 Philip M 6

Quartz | Level 8

Re: group wise sequence

Thank you for solution accepted

Super User

Re: group wise sequence

Since a DATA _NULL_ step will not create a dataset, OUTPUT is useless.

``````data _null_;
set sashelp.class;
where sex = 'M' ;
group_seq + 1;
do i = 1 to 5 ;
put @5 name @20 sex  @28 group_seq;
end;
run;``````

The SUM statement implies a RETAIN, and the variable is initialized to zero.

Quartz | Level 8

Re: group wise sequence

Thank you for your solution

Discussion stats
• 4 replies
• 456 views
• 0 likes
• 3 in conversation