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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

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

4 REPLIES 4
PGStats
Opal | Level 21

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
Jagadishkatam
Amethyst | Level 16

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
archcnu
Calcite | Level 5

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;

ballardw
Super User

What if X has two rows with the value 21?

sas-innovate-2024.png

Today is the last day to save with the early bird rate! Register today for just $695 - $100 off the standard rate.

 

Plus, 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
  • 4 replies
  • 32840 views
  • 7 likes
  • 5 in conversation