DATA Step, Macro, Functions and more

How to replace the value..

Accepted Solution Solved
Reply
Contributor
Posts: 70
Accepted Solution

How to replace the value..

Hi All,

I have a data like mentioned below..

data test;

input name $ age;

cards;

aa .

bb 1

cc 2

dd 3

ee .

ff 5

run;

I have to replace the values of age..that means second value should be in first place, 3rd should be in 2nd, 4th should be in  3rd..so on .. Please help me..

Output should be like below/..

Name age

aa 1

bb 2

cc 3

dd .

ee 5

ff .

Thanks

Yaswanth


Accepted Solutions
Solution
‎12-27-2012 06:08 AM
Respected Advisor
Posts: 3,908

Re: How to replace the value..

Sounds like an excercise so instead of simply asking for the question and learning nothing you should show us what you've already done and where you've got stuck. Just getting solutions won't help you to become a skilled developer.

But o.k., here you go. If it's study: Make sure you understand 100% what's going on here.

data want;

  merge test(keep=name) test(keep=age firstobs=2);

run;

View solution in original post


All Replies
Solution
‎12-27-2012 06:08 AM
Respected Advisor
Posts: 3,908

Re: How to replace the value..

Sounds like an excercise so instead of simply asking for the question and learning nothing you should show us what you've already done and where you've got stuck. Just getting solutions won't help you to become a skilled developer.

But o.k., here you go. If it's study: Make sure you understand 100% what's going on here.

data want;

  merge test(keep=name) test(keep=age firstobs=2);

run;

Super Contributor
Posts: 276

Re: How to replace the value..

Hi Patrick

Your solutions looks smart and easy for above post and bellow one too..

But in the context of Huge data,is above method help full?

It might take lot time to process right?

Clarify me...

Thanks

Sanjeev.K

Contributor
Posts: 70

Re: How to replace the value..

Hi Patrick..

your Mentioned code is working fine ..and also working for below post..

I tried with your code it is working..but it is taking long time when i execute for huge data in V9.1, the folk sanjeev mentioned in this post..

Could you please let me know, is there any other ways to do..with out using merge statement.

Thanks & Regards,

Yaswanth

Respected Advisor
Posts: 3,124

Re: How to replace the value..

Well, Merge seems to me very efficient if it does not look to match By variables like in this case. Another look-ahead alternative is to try SET statement:

data want;

  set test(keep=name);

  set test(keep=age firstobs=2) test(obs=1 drop=_all_);

run;

Haikuo

Contributor
Posts: 45

Re: How to replace the value..

Here is another approach, almost as efficient as Hai.Kuo's:

data new(keep=name age);

     dsid=open("test");

     nrows=attrn(dsid,"NOBS");

          do k=1 to nrows;

               rc1=fetchobs(dsid,k);

               name=getvarc(dsid,1);

               rc2=fetchobs(dsid,k+1);

               age=getvarn(dsid,2);

               output;

          end;

run;

Super User
Posts: 9,691

Re: How to replace the value..

Or put lag() function on variable name.

data test;
input name $ age;
cards;
aa .
bb 1
cc 2
dd 3
ee .
ff 5
;
run;
data want;
 set test end=last;
 _name=lag(name);
if _n_ gt 1  then do;output; 
                    if last then do;_name=name;age=.;output;end;
end;
drop name;

run;

KSharp

🔒 This topic is solved and locked.

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

Discussion stats
  • 6 replies
  • 370 views
  • 1 like
  • 6 in conversation