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

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

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

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;

View solution in original post

8 REPLIES 8
art297
Opal | Level 21

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;

Ksharp
Super User

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

art297
Opal | Level 21

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.

Ksharp
Super User

Hi.Art.  I understand your code.

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

Ksharp

TDP
Calcite | Level 5 TDP
Calcite | Level 5

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"

art297
Opal | Level 21

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;

TDP
Calcite | Level 5 TDP
Calcite | Level 5

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.

Ksharp
Super User

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

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!

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
  • 8 replies
  • 907 views
  • 0 likes
  • 3 in conversation