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 |
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
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
Hi @ballardw
Thank you for solution accepted
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.
Thank you for your solution
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
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.