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

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

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

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

6 REPLIES 6
Patrick
Opal | Level 21

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;

kuridisanjeev
Quartz | Level 8

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

yaswanthj
Fluorite | Level 6

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

Haikuo
Onyx | Level 15

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

joehinson
Calcite | Level 5

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;

Ksharp
Super User

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

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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