BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
pavank
Quartz | Level 8
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
ballardw
Super User

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

 

 

 

View solution in original post

4 REPLIES 4
ballardw
Super User

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

 

 

 

pavank
Quartz | Level 8

Hi @ballardw 

Thank you for solution accepted

Kurt_Bremser
Super User

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.

pavank
Quartz | Level 8

Hi @Kurt_Bremser 

Thank you for your solution

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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
  • 4 replies
  • 653 views
  • 0 likes
  • 3 in conversation