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

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). 

 

1 ACCEPTED SOLUTION

Accepted Solutions
gskn
Obsidian | Level 7

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

4 REPLIES 4
gskn
Obsidian | Level 7

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;

WarrenKuhfeld
Ammonite | Level 13
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;
gskn
Obsidian | Level 7

This solution is better. thank you. 

Astounding
PROC Star

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;

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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