I have a data file where one of the variables has a value of -1 0 1
for example,
a
1
1
1
1
1
-1
-1
-1
-1
0
0
0
I would like to receive
a
1
-1
1
-1
1
-1
1
-1
1
0
0
0
Thank you for your help.
You want reorder your dataset so that first 1s and -1s come alternately, and at the end all the 0s ?
data want;
do until(eof1 and eof_1);
if not eof1 then do;
set have(where=(a=1)) end=eof1;
output;
end;
if not eof_1 then do;
set have(where=(a=-1)) end=eof_1;
output;
end;
end;
do until(eof0);
set have(where=(a=0)) end=eof0;
output;
end;
stop;
run;
You would need to manually put something in your data to sort like that as there's no "logical" sort order. Why do you want the negatives interleaved with the 1s, is that because of other variables? If so include those variables in the sort.
You want reorder your dataset so that first 1s and -1s come alternately, and at the end all the 0s ?
data want;
do until(eof1 and eof_1);
if not eof1 then do;
set have(where=(a=1)) end=eof1;
output;
end;
if not eof_1 then do;
set have(where=(a=-1)) end=eof_1;
output;
end;
end;
do until(eof0);
set have(where=(a=0)) end=eof0;
output;
end;
stop;
run;
perfect solution
I have one more question if you can do it this way
1
1
-1
-1
1
1
-1
-1
0
0
0
0
or
1
1
1
1
-1
-1
-1
-1
1
1
1
1
-1
-1
-1
-1
0
0
0
0
and so on
Thank you for your help.
Is this a homework?
data want;
numConsec=2;/*set this to 1, 2 or 4 ...*/
do until(eof1 and eof_1);
do i=1 to numConsec;
if not eof1 then do;
set have(where=(a=1)) end=eof1;
output;
end;
end;
do i=1 to numConsec;
if not eof_1 then do;
set have(where=(a=-1)) end=eof_1;
output;
end;
end;
end;
do until(eof0);
set have(where=(a=0)) end=eof0;
output;
end;
stop;
run;
not tested!
If am not wrong, you want to interleave -1s between 1s . Let's add one more variable , assign consecutive odd numbers to -1s and even numbers to 1s and sort using that variable
data
have
;
input a ;
cards;
1
1
1
1
1
-1
-1
-1
-1
0
0
0
;
run;
data
want(drop = odd even )
;
set
have
;
retain odd 1 even 2;
if a = 1 then do;
odd = odd + 2;
sort_var = odd;
output;
end;
else if a = -1 then do;
even = even +2 ;
sort_var = even;
output;
end;
else do;
sort_var = 1000000;
output ;
end;
run;
proc sort data = want; by sort_var;run;
proc print data = want ;run;
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.
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.