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

program to disply sentance with n words as below in dataset variable ?

this
this is
this is sas
this is sas class.................n

 

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

How do you like this?

data test(drop=str);
str="We do not need to specify the length of a character variable if we are happy with the length it is given automatically";
do _n_=1 by 1 until(new=str);
  new=catx(' ',new,scan(str,_n_));
  output;
end;
run;

Interestingly, variable NEW seems to inherit the type and length of STR because of the UNTIL condition. Without that, it would be created as a numeric variable because of its uninitialized occurrence in the second argument of CATX. If it didn't occur there either, it would get a default length of $200 (see documentation of the CATX function).

View solution in original post

9 REPLIES 9
sonuvarsha
Calcite | Level 5

 

this is the sentance r else you dont know the length of the sentance ...the words should display as 1st ,1st 2nd,1st 2nd 3rd ...........n

in a dataset with new variable 

example

 

x='this is sas class'

the output should be like  below 

 

 new

______

this 

this is 

this is sas

this is sas class

mohamed_zaki
Barite | Level 11

One way for that 

data null;
length x $20;
retain x " ";
str="this is sas class";
do i = 1 to COUNTW(str) by 1;
   	x= strip(x) || " "||scan(str,i,' ') ;
   	output;
end;
run;
sonuvarsha
Calcite | Level 5

can we write the same program using other functions like anyspace , substr, compress,find  functions ?n length shuld be the default  length of the string to the new variable 

sonuvarsha
Calcite | Level 5

because everytime we have to write length statement before if we increase that string word count......if string length is unkown then ??? that is my question 

Kurt_Bremser
Super User

Setting the length is necessary. If you need to automate that, you can read the length of the input variable beforehand from sashelp.vcolumn and use that to set the length in the final data step.

FreelanceReinh
Jade | Level 19

How do you like this?

data test(drop=str);
str="We do not need to specify the length of a character variable if we are happy with the length it is given automatically";
do _n_=1 by 1 until(new=str);
  new=catx(' ',new,scan(str,_n_));
  output;
end;
run;

Interestingly, variable NEW seems to inherit the type and length of STR because of the UNTIL condition. Without that, it would be created as a numeric variable because of its uninitialized occurrence in the second argument of CATX. If it didn't occur there either, it would get a default length of $200 (see documentation of the CATX function).

mohamed_zaki
Barite | Level 11

This would work too

data have;;
str="this is sas class";
x=str;
do i = 1 to COUNTW(str) by 1;
	if i=1 then x="";
   	x= strip(x) || " "||scan(str,i,' ') ;
   	output;
end;
run;

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 9 replies
  • 2140 views
  • 3 likes
  • 4 in conversation