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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
gergely_batho
SAS Employee

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;

View solution in original post

5 REPLIES 5
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

gergely_batho
SAS Employee

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;

makset
Obsidian | Level 7

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.

gergely_batho
SAS Employee

Is this a homework? Smiley Happy

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!

Karthikeyan
Fluorite | Level 6

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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

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.

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
  • 5 replies
  • 1043 views
  • 2 likes
  • 4 in conversation