BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.

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

1 ACCEPTED SOLUTION

Accepted Solutions
sveinola_gundhus_tv2_no
Fluorite | Level 6

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

7 REPLIES 7
Astounding
PROC Star

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.

sveinola_gundhus_tv2_no
Fluorite | Level 6

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

 

 

Reeza
Super User

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;
Astounding
PROC Star

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?

sveinola_gundhus_tv2_no
Fluorite | Level 6

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

Astounding
PROC Star

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.

 

sveinola_gundhus_tv2_no
Fluorite | Level 6

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

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
  • 7 replies
  • 991 views
  • 2 likes
  • 3 in conversation