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: Call for Content

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!

Submit your idea!

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
  • 4 replies
  • 434 views
  • 0 likes
  • 3 in conversation