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
Rhodochrosite | Level 12
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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 4 replies
  • 887 views
  • 1 like
  • 3 in conversation