Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- SAS Procedures
- /
- Rearranging values under a variable

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

10-20-2011 03:10 PM

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

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

10-21-2011 08:22 AM

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

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

10-20-2011 03:24 PM

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;

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

10-20-2011 10:39 PM

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

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Ksharp

10-20-2011 11:49 PM

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.

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to art297

10-21-2011 03:32 AM

Hi.Art. I understand your code.

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

Ksharp

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to art297

10-21-2011 07:15 AM

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

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

10-21-2011 08:22 AM

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;

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to art297

10-23-2011 10:49 PM

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.

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

10-23-2011 09:44 PM

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