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-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!

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