- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Posted 09-16-2009 01:56 PM
(2895 views)
Could anyone briefly explain what the !! does with the subst function?
8 REPLIES 8
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
!! 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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
Scott Barry
SBBWorks, Inc.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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)
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)
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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 (phone:) (=phone.);
16 run;
phonenumber=(312)555-1212
[/pre]
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 (phone:) (=phone.);
16 run;
phonenumber=(312)555-1212
[/pre]
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
sorry that is 2 space before the 3
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
Scott Barry
SBBWorks, Inc.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
the answer is ( 3) [2 spaces in front of the 3]
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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!
makes sense!