Learning SAS? Welcome to the exclusive online community for all SAS learners.

how to change the row values

Accepted Solution Solved
Reply
Contributor
Posts: 42
Accepted Solution

how to change the row values

Hi friends,

              I have  a problem.

plz help me.

I have data like this

       center_name

           x

           lalitha

           y

          lalitha

          z

          lilitha

here x y z are the center names  and 'lalitha'  sub_cente name .

      here i pick lalitha .useing where condition i will get the 3 observations .

 

  

then i want output like this

cente_name

       x

       x_lalitha

       y

       y_lalitha

       z

       z_lalitha


Accepted Solutions
Solution
‎03-23-2015 09:53 AM
Frequent Contributor
Posts: 115

Re: how to change the  row values

Posted in reply to tlnarayana26

data have;

input center_name : $20.;

datalines;

abc

def

xx

lalitha

y

lalitha

zzz

lalitha

sss

lalitha

;

data want;

set have;

l=lag(center_name);

if center_name='lalitha' then center_name=catx('_',l,center_name);

drop l;

run;

View solution in original post


All Replies
Super User
Posts: 5,429

Re: how to change the row values

Posted in reply to tlnarayana26

What kind of data is this? Can you be certain that the structure always will like this? You have no id-variable.

Anyhow, take a look at RETAIN, and using _N_ or some other technique to fins out if you are on a odd/even row number.

Data never sleeps
Contributor
Posts: 42

Re: how to change the row values

this data always like this.

       center_name

           x

           lalitha

           y

          lalitha

          z

          lilitha

some times sub_center names are added like this

       center_name

           x

           lalitha

           y

          lalitha

          lalitha2

          z

          lalitha

          ramu

          lucky

its contains only one cloumn and no other cloumns

Super User
Posts: 5,509

Re: how to change the row values

Posted in reply to tlnarayana26

This should work:

data want;

  set have;

  if lengthn(center_name)=1 then prefix=center_name;

  else center_name = trim(prefix) || '_' || center_name;

  retain prefix;

  drop prefix;

run;

It does assume that CENTER_NAME is long enough that you can add a couple of characters and not exceed the available length.

Good luck.

Contributor
Posts: 42

Re: how to change the row values

Posted in reply to Astounding

hi sir

  length is not same  for center name

data like this

center name

    abc

    def

    xx

    lalitha

     y

     lalitha

     zzz

     lalitha

      sss

     lalitha

here lalitha repeated  value

then out put like this

center name

abc

def

xx

xx_lalitha

y

y_lalitha

zzz

zzz_lalitha

sss

sss_lalitha

Super User
Posts: 5,509

Re: how to change the row values

Posted in reply to tlnarayana26

So how do you know which CENTER_NAME values are a prefix, and which are a suffix?  Is anything with a length of 3 or less a prefix?  Is "lalitha" the only suffix?  Just what are the rules?  The programming will likely be easy but the rules are harder.

Contributor
Posts: 42

Re: how to change the  row values

Posted in reply to Astounding

hi sir 

any where lalitha is presented that row should  be  updated with the aboverow_lalitha

Solution
‎03-23-2015 09:53 AM
Frequent Contributor
Posts: 115

Re: how to change the  row values

Posted in reply to tlnarayana26

data have;

input center_name : $20.;

datalines;

abc

def

xx

lalitha

y

lalitha

zzz

lalitha

sss

lalitha

;

data want;

set have;

l=lag(center_name);

if center_name='lalitha' then center_name=catx('_',l,center_name);

drop l;

run;

Contributor
Posts: 42

Re: how to change the  row values

Posted in reply to naveen_srini

hi sir ,

thank you

code working. i will get the results

🔒 This topic is solved and locked.

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

Discussion stats
  • 8 replies
  • 544 views
  • 0 likes
  • 4 in conversation