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).
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;
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;
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;
This solution is better. thank you.
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;
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!
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.
Ready to level-up your skills? Choose your own adventure.