Hi,
I have a data set with 1 variable and 3 inputs which is as follow:-
VarName
1) Jack
2) Alfred Crooks
3) Julia
I want to store "Jack" as "Jack " (One trailing space in the end) and rest of the inputs to remain as it is in sas data set. can this be done in SAS ? I have tried different function tranwrd, cat, || , also through if else condition but nothing worked. It would be great if anyone can guide me on this.
Thanks.
How do you know it "didn't work"?
By default most of the procedures creating output will not display a leading space.
And on potential issue is that depending on the assigned length of a variable if you attempt to add a leading space the result might not fit in allowed space for that defined variable.
Run this code;
data example; x='Jack'; y=' '||x; z=cat(' ',x); ly=length(y); lz=length(z); run; /* look at defined length for x in the output*/ proc contents data=example; run; data example2; set example ; /* attempt to insert space in existing variable*/ x=' '||x; run;
If you print the Example data set you will see that the value for x is "Jack". In the second set print will only show "Jac" but opening the set in the table viewer you may be able to tell that there is a space and the K is no longer there because the "space" makes the value too long to fit in the 4 spaces.
Hi @karan96,
@karan96 wrote:
I want to store "Jack" as "Jack " (One leading space in the end)
What you describe is rather a trailing blank. If your character variable needs to accommodate a value like "Alfred Crooks" it must have a length of at least 13. Then an observation containing the name "Jack" will actually contain the string "Jack " with at least 9 (=13-4) trailing blanks -- assuming a traditional (fixed-width) character variable, not the newer VARCHAR data type.
Note that trailing blanks are ignored in character comparisons, e.g., the condition "Jack" = "Jack " is true. But in other situations like concatenating character values (e.g., name=firstname||lastname [not recommended]) or using certain character functions the trailing blanks must not be forgotten.
No.
SAS has two variable types. Floating point numbers and fixed length character strings.
To distinguish 'Jack ' from 'Jack' you will need to store extra information somehow. Either include something in the variable itself, like quotes or a delimiter or stop character.
data test;
input string $40.;
cards;
"Jack "
Alfred Crooks
Julia
;
Or store the desired length in a separate variable.
data test;
input length string $40.;
cards;
5 Jack
13 Alfred Crooks
5 Julia
;
Hello @karan96
You can have a trailing space but with single quote. Thus for Jack it will be 'Jack ' but not "Jack ".
The output will be like this. There is a trailing space at the end of each variable.
The code is
data test;
input Name $40.;
datalines;
Jack
Alfred Crooks
Julia
;
run;
data test;
set test;
Name2="'"||strip(name)||" '";
run;
proc print;
run;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
Check out this tutorial series to learn how to build your own steps in SAS Studio.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.