Help using Base SAS procedures

!! With subst function

Reply
Occasional Contributor
Posts: 8

!! With subst function

Could anyone briefly explain what the !! does with the subst function?
Super Contributor
Posts: 394

Re: !! With subst function

Posted in reply to Stephanie55901
Do you mean substr instea of subst?

!! is the concatenation operator, so an expression of the form

charvar3 = charvar1 !! substr(charvar2, 1, 1);

concatenates the leftmost character from charvar2 to the right end of charvar1 to produce charvar3 (assuming charvar3 is long enough to hold the result of the concatenation).

See http://support.sas.com/documentation/cdl/en/lrcon/61722/HTML/default/a000780367.htm.
Super Contributor
Super Contributor
Posts: 3,174

Re: !! With subst function

The key point made by Tim@SAS is that the charvar1 variable is not going to be trimmed of any trailing blanks -- if this behavior is undesired with your programming needs, you may want to consider also using either the CATT function (new with SAS 9) or using TRIM(charvar1) or possibly if you expect some particular length using the PUT(charvar1,) as well.

Scott Barry
SBBWorks, Inc.
Occasional Contributor
Posts: 8

Re: !! With substr function

Posted in reply to Stephanie55901
thanks. I think i usually use the || for this. I am studying for the BASE certification exam, and there was a question on the practice exam , there was a question:

data hello;
phonenumber=3125551212;
code='(' !! substr(Phonenumber,1,3) !! ')';
run;

i totally understand what the substr function does, just stuck sometimes on what is trimmed

the answer is that code= ( 3)
Respected Advisor
Posts: 3,799

Re: !! With substr function

Posted in reply to Stephanie55901
You problems are caused by the data type, numeric, for PHONENUMBER and the conversion to character using BEST12. format.

How about a different method.

[pre]
9 proc format;
10 picture phone other=' 999)999-9999' (prefix='(');
NOTE: Format PHONE has been output.
11 run;

NOTE: PROCEDURE FORMAT used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds


12 data _null_;
13 phonenumber=3125551212;
14 *code='(' !! substr(Phonenumber,1,3) !! ')';
15 put (phoneSmiley Happy (=phone.);
16 run;

phonenumber=(312)555-1212
[/pre]
Occasional Contributor
Posts: 8

Re: !! With subst function

Posted in reply to Stephanie55901
sorry that is 2 space before the 3
Super Contributor
Super Contributor
Posts: 3,174

Re: !! With subst function

Posted in reply to Stephanie55901
What is the answer? Not seeing the question but seeing the intended SAS code, it appears to me that someone is attempting to extract the area code an enclose it in parentheses to create SAS variable "code", right?

Scott Barry
SBBWorks, Inc.
Occasional Contributor
Posts: 8

Re: !! With subst function

Posted in reply to Stephanie55901
the answer is ( 3) [2 spaces in front of the 3]
Occasional Contributor
Posts: 8

Re: !! With subst function

Posted in reply to Stephanie55901
thanks, that helps alot to know that when SAS converts automatically to character, it converts to the best12. and right justifies, since the phone number is currently 10 digits, right justifying puts 2 trailing blanks in front and hence the 2 extra spaces.

makes sense!
Ask a Question
Discussion stats
  • 8 replies
  • 195 views
  • 0 likes
  • 4 in conversation