Dear All,
i new to sas. i am learning sas . i am exploring some sas functions.
i am testing scan function, it is not showing desired output.
data have;
chk="01 APR";
do i=1 to 3;
t=scan(chk,i,' ');
put t=;
g=length(t);
put g=;
if length(t) in (1 2) then d=t;
else if length(t)=3 then m=t;
else if length(t)=4 then y=t;
end;
put chk= d= m= y=;
run;
desired o/p;
chk=01 APR d=01 m=APR y=
but where as i got
chk=01 APR d=(missing) m=APR y=
it is getting puzzle to me , where i am going wrong
Thanks
Cathyn
Add some more PUT statements so you can see what is happening inside your do loop.
data have;
input chk $20.;
put (_n_ chk) (=) ;
do i=1 to 3;
t=scan(chk,i,' ');
put i= t= @;
g=length(t);
put g= @;
if length(t) in (1 2) then d=t;
else if length(t)=3 then m=t;
else if length(t)=4 then y=t;
put (d m y) (=);
end;
put ;
cards;
01 APR
02 MAY 2014
run;
Perhaps you want to use the LENGTHN() function instead?
Add some more PUT statements so you can see what is happening inside your do loop.
data have;
input chk $20.;
put (_n_ chk) (=) ;
do i=1 to 3;
t=scan(chk,i,' ');
put i= t= @;
g=length(t);
put g= @;
if length(t) in (1 2) then d=t;
else if length(t)=3 then m=t;
else if length(t)=4 then y=t;
put (d m y) (=);
end;
put ;
cards;
01 APR
02 MAY 2014
run;
Perhaps you want to use the LENGTHN() function instead?
two fixes, for one reason. Since you are learning (well, who isn't on this forum), I will give you the opportunities to check out some Docs yourself before giving out explanations.
data have;
chk="01 APR";
do i=1 to 3;
t=scan(chk,i,' ');
put t=;
g=length(t);
put g=;
if lengthn(t) in (1,2)
/*if length(t) in (2) */
then d=t;
else if length(t)=3 then m=t;
else if length(t)=4 then y=t;
end;
put chk= d= m= y=;
run;
Hi Cathyn,
To make it dynamic, i used the countw() which counts the number of words, separated by blank space. so once the count is available , we could use the same in do loop and can minimize the number of times the loop executes. This also corrects the problem you are facing. The use of lengthn function is also not required. Please try the below code,
data have;
chk="01 APR";
cnt=countw(chk);
do i=1 to cnt;
t=scan(chk,i,' ');
put t=;
g=length(t);
put g=;
if length(t) in (1 2) then d=t;
else if length(t)=3 then m=t;
else if length(t)=4 then y=t;
end;
put chk= d= m= y=;
run;
Thanks,
jag
TThank you all!!!!!
Before posting I used strip function to avoid trail blank. But doesn't work!!!
thank you Tom, hai.kuo and jag
cathy
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.