DATA Step, Macro, Functions and more

SET Statement - combine 2 lines/records as single record.

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 7
Accepted Solution

SET Statement - combine 2 lines/records as single record.

Hi All, 

 

Can you help me how to use SET statement to reach 2 records at a time and create single record. 

 

dataset:

var0

record1

record2

record3

record4

record5

record6

record7

record8

 

 

Output:

var1            var2

record1       record2

record3       record4

record5       record6

record7       record8

 

there is no relation between 2 records, Simply read 2 records at a time and write to output using SET statement (I have data in SAS dataset). 

 


Accepted Solutions
Solution
‎11-17-2017 04:50 PM
Occasional Contributor
Posts: 7

Re: SET Statement - combine 2 lines/records as single record.

I was able to resolved. 

 

Below is the solution:

 

Data temp1 (KEEP = N VAR0);

  set Dataset1;

  N = _N_;

  IF MOD(N,2) = 0 THEN N = N - 1;

RUN;

PROC SORT DATA TEMP1; BY N; RUN;

DATA TOLIST(KEEP = VAR1 VAR2);

    RETAIN VAR1;

    SET TEMP1;

    BY N;

    IF FIRST.N THEN VAR1 = VAR0;

    IF LAST.N THEN DO;

         VAR2 = VAR0;

         OUTPUT;

    END;

RUN;

View solution in original post


All Replies
Solution
‎11-17-2017 04:50 PM
Occasional Contributor
Posts: 7

Re: SET Statement - combine 2 lines/records as single record.

I was able to resolved. 

 

Below is the solution:

 

Data temp1 (KEEP = N VAR0);

  set Dataset1;

  N = _N_;

  IF MOD(N,2) = 0 THEN N = N - 1;

RUN;

PROC SORT DATA TEMP1; BY N; RUN;

DATA TOLIST(KEEP = VAR1 VAR2);

    RETAIN VAR1;

    SET TEMP1;

    BY N;

    IF FIRST.N THEN VAR1 = VAR0;

    IF LAST.N THEN DO;

         VAR2 = VAR0;

         OUTPUT;

    END;

RUN;

SAS Super FREQ
Posts: 503

Re: SET Statement - combine 2 lines/records as single record.

data x;
   input var0 $;
   datalines;
record1
record2
record3
record4
record5
record6
record7
record8
;

data x2;
   i = (_n_ - 1) * 2 + 1;
   set x(rename=(var0=var1)) point=i nobs=n;
   i + 1;
   set x(rename=(var0=var2)) point=i;
   output;
   if i eq n then stop;
run;
   
proc print; run;
Occasional Contributor
Posts: 7

Re: SET Statement - combine 2 lines/records as single record.

Posted in reply to WarrenKuhfeld

This solution is better. thank you. 

Super User
Posts: 6,785

Re: SET Statement - combine 2 lines/records as single record.

If your incoming data is guaranteed to contain an even number of observations, here is a simple way:

 

data want;

array var {2} $ 50;

do _n_=1 to 2;

   set have;

   var{_n_} = var0;

end;

drop var0;

run;

☑ This topic is solved.

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

Discussion stats
  • 4 replies
  • 176 views
  • 1 like
  • 3 in conversation