BookmarkSubscribeRSS Feed
Andygray
Quartz | Level 8

data have;

length var1 $ 50 ;

input var1 $ ;

datalines;

pointofinterest

;

data want;

set have;

do i= 1 to length(var1);

newvar=substr(var1,i,1);

end;

run;

The output I am getting is newvar=t

I am looking to get the entire string pointofinterest output using the loop with i value being 16. Where am i wrong?

4 REPLIES 4
SASKiwi
PROC Star

Does this do what you want?

data want;

set have;

do i= 1 to length(var1);

newvar=substr(var1,i,1);

output;

end;

run;

Andygray
Quartz | Level 8

No, not the explicit output statement..just the same pointofinterest..I am merely testing stuff to learn..it is giving me only the last letter t leaving out "pointofinteres"

Reeza
Super User

That is the correct output.

newvar is recreated  every time you go through a loop iteration. 

So your last call is newvar=substr(var1, length(var1), 1) which is the last character.

Did you want to add each letter in the loop i.e.

p

po

poi

poin

...

pointofinterest

?

If so, look into a concatenate function - CATT, CAT, CATX, CATS

Kurt_Bremser
Super User

Try

data want;

set have;

length newvar $ 50; *necessary, otherwise newvar would only have a length of 8;

do i= 1 to length(var1);

substr(newvar,i,1)=substr(var1,i,1);

end;

run;

SAS Innovate 2025: Call for Content

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!

Submit your idea!

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.

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
  • 659 views
  • 0 likes
  • 4 in conversation