Help using Base SAS procedures

how to get row number /observation number

Accepted Solution Solved
Reply
N/A
Posts: 1
Accepted Solution

how to get row number /observation number

I am new to SAS. I spent a couple of days looking for a way to return row number, but no success. I wonder whether you can kindly help me

For example, this is the data set

ID     X     Y

1      10     2

2      15     3

3      21     6

4      7     15

I need SAS to look for X=21, and return ID=3, then I can change Y[3] from 6 to 8.

This should be a simple task, but I cannot solve it.  Thanks.


Accepted Solutions
Solution
‎01-28-2015 10:44 PM
Respected Advisor
Posts: 4,918

Re: how to get row number /observation number

Two simple ways.

If you don't mind creating a new dataset:

data want;

set have;

if X=21 then Y = 8;

run;

If you don't mind using SQL:

proc sql;

update have set Y = 8 where X=21;

quit;

PG

PG

View solution in original post


All Replies
Solution
‎01-28-2015 10:44 PM
Respected Advisor
Posts: 4,918

Re: how to get row number /observation number

Two simple ways.

If you don't mind creating a new dataset:

data want;

set have;

if X=21 then Y = 8;

run;

If you don't mind using SQL:

proc sql;

update have set Y = 8 where X=21;

quit;

PG

PG
Trusted Advisor
Posts: 1,137

Re: how to get row number /observation number

Alternatively,

data have;

input ID     X     Y;

row=_n_;

if row=3 then flag=1;

if flag=1 then y=8;

cards;

1      10     2

2      15     3

3      21     6

4      7     15

;

run;

Thans,

jag

Thanks,
Jag
New Contributor
Posts: 3

Re: how to get row number /observation number

If you are looking just to return the row number or the observation number then use SAS automatic variable _N_ thats holds the observation number of the observation read.

data test;

input id x y;

if x=21 then

  do

     rowno=_n_;        /* rowno holds the observation number*/

     y = 8;                /* control is on the current observation */

  end;

cards;

1      10     2

2      15     3

3      21     6

4      7     15

;

run;

Super User
Posts: 11,335

Re: how to get row number /observation number

What if X has two rows with the value 21?

🔒 This topic is solved and locked.

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

Discussion stats
  • 4 replies
  • 5994 views
  • 7 likes
  • 5 in conversation