Given the following alphanumeric variable VAR1 :
VAR1 = "Is this the real life? Is this just fantasy? Caught in a landslide. No escape from reality!";
May I know which function yields this excepted results?
Is this the real life Is this just fantasy Caught in a landslide No escape from reality
Do you really want line breaks in values? Please explain the rules to add line breaks.
You can try this:
data narf;
length var1 var2 $ 100;
VAR1 = "Is this the real life? Is this just fantasy? Caught in a landslide. No escape from reality!";
var2 = prxchange('s/[[:punct:]]/^n/', -1, var1);
run;
ods escapechar='^';
proc print;run;
@andreas_lds Thanks. I need line breaks when there is any special character in the string as I shown in example. Can't we achieve it using scan function or something similar instead of prxchange?
You can insert line breaks into the variable by using TRANSLATE:
data want;
VAR1 = "Is this the real life? Is this just fantasy? Caught in a landslide. No escape from reality!";
var1 = translate(var1,'0a0a0a'x,'.!?');
checkvar = put(var1,$hex200.);
put checkvar=;
run;
data _null_;
set want;
file '/folders/myfolders/test.txt';
put var1;
run;
Note that the character used will depend on the environment used to open the file; I use CR because I work on a Mac and inspected the file with TextMate and TextEdit. Since I actually used LF (0A) already, this should be the way to go.
You may find that LF (0Ax) is more appropriate for things like Excel.
Also note that SAS will not show the line breaks when viewing the file or creating output (e.g. with PROC PRINT).
@David_Billa wrote:
@andreas_lds Thanks. I need line breaks when there is any special character in the string as I shown in example. Can't we achieve it using scan function or something similar instead of prxchange?
If you want to code the loop manually, countw+scan could be used, but why? You can, of course, replace [[:punct:]] with "any special char", some need to be escaped with \, like the dot.
COUNTW and SCAN, with the proper selection of delimiters:
data want;
VAR1 = "Is this the real life? Is this just fantasy? Caught in a
landslide. No escape from reality!";
do i = 1 to countw(var1,".!?");
text = scan(var1,i,".!?");
output;
end;
keep text;
run;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.