Solved
New Contributor
Posts: 3

# Rearranging values under a variable

I have a dataset with about 10 variables in it that I am creating from an input pipe delimited file. The problem is one of the values is at the bottom of the dataset (obs 4) and I need it to be at (obs 2) for example. Is there a dataset step that I could use find this specific row and move it to the correct obs?

Here is an example to show what I am saying. I would like to move "total     4     2     3"  to the second line.

header    a        b       c

5           8        4       8

67          7        4        9

3         11        4      0

total        4        2      3

Accepted Solutions
Solution
‎10-21-2011 08:22 AM
PROC Star
Posts: 8,169

## Rearranging values under a variable

The code either does what you want or I don't understand what you want.  The code keeps the first obs as order=_n_, which would be one.  Then, it sets all of the other records values for order to equal _n_+1, or one greater than their current location.  Then, finally, it sets the last record to have a value of order equal to 2.

Then, when sorted, you get the order I think that you were looking for.

data have;

input header \$ a  b  c;

cards;

5           8        4       8

67          7        4        9

3         11        4      0

total        4        2      3

;

data want;

set have end=eof;

if _n_ eq 1 then order=_n_;

else order=_n_+1;

if eof then order=2;

run;

proc sort data=want out=want (drop=order);

by order;

run;

proc print data=want;

run;

All Replies
PROC Star
Posts: 8,169

## Rearranging values under a variable

You could always use a multidimensional array to manipulate a file, but I think that the following would be a lot easier:

data have;

input header \$ a  b  c;

cards;

5           8        4       8

67          7        4        9

3         11        4      0

total        4        2      3

;

data want;

set have end=eof;

order=_n_+1;

if eof then order=1;

run;

proc sort data=want out=want (drop=order);

by order;

run;

Super User
Posts: 10,787

## Rearranging values under a variable

What logic is that  (obs 4) moved to be at (obs 2).

PROC Star
Posts: 8,169

## Rearranging values under a variable

Was that a question for me or the OP?  I took the header to represent obs 1.  If that assumption was not correct, one would only have to use something like:

if _n_ eq 1 then order=1;

else order=_n_+1;

if eof then order=2;

However, I'm not sure what the OP really wants.

Super User
Posts: 10,787

## Rearranging values under a variable

Hi.Art.  I understand your code.

It is for OP. I do not understand which obs4 moves to be at which obs2.

Ksharp

New Contributor
Posts: 3

## Rearranging values under a variable

Sorry for any confusion. The line "header   a    b   c" are the variable names only. The four lines below are the values in the dataset under each variable (so there are only 4obs in this example). i appreciate the help, but like Ksharp said I do not understand how this will move the last row which is "obs4" up so that it would be "obs2"

Solution
‎10-21-2011 08:22 AM
PROC Star
Posts: 8,169

## Rearranging values under a variable

The code either does what you want or I don't understand what you want.  The code keeps the first obs as order=_n_, which would be one.  Then, it sets all of the other records values for order to equal _n_+1, or one greater than their current location.  Then, finally, it sets the last record to have a value of order equal to 2.

Then, when sorted, you get the order I think that you were looking for.

data have;

input header \$ a  b  c;

cards;

5           8        4       8

67          7        4        9

3         11        4      0

total        4        2      3

;

data want;

set have end=eof;

if _n_ eq 1 then order=_n_;

else order=_n_+1;

if eof then order=2;

run;

proc sort data=want out=want (drop=order);

by order;

run;

proc print data=want;

run;

New Contributor
Posts: 3

## Rearranging values under a variable

Thank you art297...I used your method to figure it out. At first I didn't quite understand the concept but I got it now.

Super User
Posts: 10,787

## Rearranging values under a variable

Do you mean to move the last obs  where the second obs is ?

then what about the second obs? keep it or delete it? The following is keeping it.

```data have;
input header \$ a  b  c;
cards;
5           8        4       8
67          7        4        9
3         11        4      0
total        4        2      3
;
run;
data want;
if _n_ eq 2 then do;set have point=_nobs nobs=_nobs;output;end;
set have end=last;
if last then stop;
output;
run;
```

Ksharp

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
• 8 replies
• 205 views
• 0 likes
• 3 in conversation