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

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

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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?

View solution in original post

4 REPLIES 4
Tom
Super User Tom
Super User

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?

Haikuo
Onyx | Level 15

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;

Jagadishkatam
Amethyst | Level 16

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

Thanks,
Jag
cathy_sas
Calcite | Level 5

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 955 views
  • 6 likes
  • 4 in conversation