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
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;
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;
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
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
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;
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
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!
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.
Ready to level-up your skills? Choose your own adventure.