Retain every other obs with no by group

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 10
Accepted Solution

Retain every other obs with no by group

Hi

I have a dataset where I need to combine values from every other obs and I have no by group.

 

Your advice is much obliged.

 

DATA Have;
    input Subject $ 1-16 Status $ 17-30  ;
    cards;
some info           Success
some other innfo             
some info           Success
new info                   
RUN;

 

DATA Want;
    input Subject $ 1-16 Status $ 17-30  NewVar $ 31-50;
    cards;
some info           Success   some other innfo    
some info           Success   new info          
RUN;

 

Thank you


Accepted Solutions
Solution
‎03-08-2016 04:19 PM
Occasional Contributor
Posts: 10

Re: Retain every other obs with no by group

Thank you all for your input.

 

My preferred solution is

 

data want;
merge have (keep=Subject Status Date)
           have (keep=Subject rename=subject=newvar firstobs=2);
if mod(_n_, 2) = 1;
run;

 

Im not investigating the retain track..

 

Thank you

View solution in original post


All Replies
Super User
Posts: 5,085

Re: Retain every other obs with no by group

The INPUT statement allows you to move on to the next line of data.  For example, either of these would be possible:

 

input Subject $ 1-16 Status $ 17-30 / NewVar $ 1-20;

 

input Subject $ 1-16 Status$ 17-30 / NewVar $ 31-50;

 

The slash indicates that the INPUT statement should move to the next line.  You just have to complete the INPUT statement so that SAS knows where to find NewVar on the second line.

 

Good luck.

Occasional Contributor
Posts: 10

Re: Retain every other obs with no by group

Thank you for the suggestion. I will try to work on this.

 

My problem was unfortunately not very well articulated.

 

The data Have is a large dataset and the Want is what I was thinking is possible to produce in a datastep using some kind of retain?

I have not done much retain code and struggle with this challenge.

 

Advice on such a datastep with a retain statement (maybe do loops?) code is also much obliged

 

 

Super User
Posts: 17,865

Re: Retain every other obs with no by group

Merge the data with itself, renaming the variable and starting from the second observation.

Also, drop every other record, or keep every other record depending on how you look at it.

 

data want;
merge have 
      have (keep=subject rename=subject=newvar firstobs=2);
if mod(_n_, 2) = 1;
run;
Super User
Posts: 5,085

Re: Retain every other obs with no by group

We might have to go back and re-define the starting point.  

 

Is your large data set already a SAS data set, or is it still text?  

 

If it's a SAS data set, what does it look like?  

 

Is it more important to use RETAIN, vs. finding a simple solution that works?

Occasional Contributor
Posts: 10

Re: Retain every other obs with no by group

Hi

I found that the merge with itself works fine with this tuning

 

data want;
merge have (keep=Subject Status Date)
           have (keep=Subject rename=subject=newvar firstobs=2);
if mod(_n_, 2) = 1;
run;

 

If retain is possible I would love to learn howto..

 

It is a SAS Dataset and looks like this:

 

SubjectStatusDate
Feilkode 6013Success10Feb2016 18:21:40
Result: Created reply #8974223 to ticket #ASV-223434  
Får feilmelding 3141Success10Feb2016 16:36:06
Result: Created reply #1215534 to ticket #ORI-985743  
Feilmelding Lag - Lag borte06.02.2016Success10Feb2016 14:17:17
Result: Created reply #12345665to ticket #YXM-95543553  
Får feilmelding 3141Success09Feb2016 20:45:20
Result: Created reply #459345783to ticket #ORI-31254345  

 

The subject starting with Result: should be a new variable in the above line.

 

Retain where Status is missing is what I have tried.

 

Thank you for the efforts

Super User
Posts: 5,085

Re: Retain every other obs with no by group

If you must use RETAIN, here is a starting point:

 

data want;

infile rawdata;

if mod(_n_, 1) then input Subject $ 1-16 Status $ 17-30;

else do;

   input NewVar $ 1-20;

   output;

end;

run;

 

You will need to add a RETAIN statement to get this to work properly.

 

Solution
‎03-08-2016 04:19 PM
Occasional Contributor
Posts: 10

Re: Retain every other obs with no by group

Thank you all for your input.

 

My preferred solution is

 

data want;
merge have (keep=Subject Status Date)
           have (keep=Subject rename=subject=newvar firstobs=2);
if mod(_n_, 2) = 1;
run;

 

Im not investigating the retain track..

 

Thank you

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 461 views
  • 2 likes
  • 3 in conversation