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 ds;
set sashelp.class;
if mod(_n_,2)=1 then seq=1;output;
else  mod(_n_,2)=0 then seq=1+1;

    output;
keep name seq;
proc print noobs;
run;

required output
Name Seq
Alfred 1
Alice 1
Alice 1
Barbara 2
Carol 2
Carol 2
Henry 3
James 3
James 3
Jane 4
Janet 4
Janet 4
Jeffrey 5
John 5
John 5
Joyce 6
Judy 6
Judy 6
Louise 7
Mary 7
Mary 7
Philip 8
Robert 8
Robert 8
Ronald 9
Thomas 9
Thomas 9
William 10

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

Making reasonable assumptions about what the input looks like, here's what I came up wth:

data want;
   set have;
   if mod(_n_, 2) then do;  
      seq + 1;
      output;
   end;
   else do;
      output;
      output;
   end;
run;

So you are usig a lot of the right tools, but definitely need to tweak the program to get the desired result.

View solution in original post

3 REPLIES 3
Astounding
PROC Star

Making reasonable assumptions about what the input looks like, here's what I came up wth:

data want;
   set have;
   if mod(_n_, 2) then do;  
      seq + 1;
      output;
   end;
   else do;
      output;
      output;
   end;
run;

So you are usig a lot of the right tools, but definitely need to tweak the program to get the desired result.

PaigeMiller
Diamond | Level 26

Could you explain the desired output? It appears that you want Alfred once, Alice twice, Barbara once, Carol twice, and so on. What is the logic?

--
Paige Miller
mkeintz
PROC Star

I see you already have a solution.  Here's is an alternative that eschews DO groups:

 

data want;
  set have;
  seq+mod(_n_,2);
  output;
  if mod(_n_,2)=0 then output;
run;
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 3 replies
  • 560 views
  • 0 likes
  • 4 in conversation