t*****2 发帖数: 94 | 1 QUESTION 72
The following SAS program is submitted:
data work.test;
First='Ipswich, England';
City=substr(First,1,7);
City_Country=City ||', '||'England';
run;
L1= length(City_Country);
L2= lengthc(City_Country);
答案是: L1=25, L2=25
但是把程序改成如下:
City_Country=substr(First,1,7)||', '||'England';
L1= length(City_Country);
L2= lengthc(City_Country);
答案是:L1=16, L2=25
我就怀疑有trailing, 所以试了newname=trim(City_Country);但是长度还是25.我就搞
不清楚这个16是什么情况。
我快疯掉了。帮帮忙! | o*******w 发帖数: 2310 | 2 City=substr(First,1,7)
the city will have the same length as First=16 because of the substr;
the substr(First,1,7)=7 bytes so City_Country=substr(First,1,7)||', '||'
England'has length 7+1+8=16. | d******9 发帖数: 404 | 3 Pat pat.... The difference is if you create a new variable or not.
In the Case 1,
First='Ipswich, England';
City=substr(First,1,7);
City_Country=City ||', '||'England';
The results of SUBSTR function is assigned to the newly created variable
city, therefore the length of city is 16, same as First by default.
Thus, the length of City_Country is: 16 + 2 + 7=25.
In Case 2, you did not create a new variable when use SUBSTR,
City_Country=substr(First,1,7)||', '||'England';
Thus, the length of City_Country is: 7 + 2 + 7= 16.
BAO ZI, please | o*******w 发帖数: 2310 | 4 good!
【在 d******9 的大作中提到】 : Pat pat.... The difference is if you create a new variable or not. : In the Case 1, : First='Ipswich, England'; : City=substr(First,1,7); : City_Country=City ||', '||'England'; : The results of SUBSTR function is assigned to the newly created variable : city, therefore the length of city is 16, same as First by default. : Thus, the length of City_Country is: 16 + 2 + 7=25. : In Case 2, you did not create a new variable when use SUBSTR, : City_Country=substr(First,1,7)||', '||'England';
| o*******w 发帖数: 2310 | 5 what is your test date?
【在 t*****2 的大作中提到】 : QUESTION 72 : The following SAS program is submitted: : data work.test; : First='Ipswich, England'; : City=substr(First,1,7); : City_Country=City ||', '||'England'; : run; : L1= length(City_Country); : L2= lengthc(City_Country); : 答案是: L1=25, L2=25
| d******9 发帖数: 404 | 6 Sorry, I was wrong.
The true reason is because of the below notes from SAS support:
Comparisons
*
The LENGTHC function returns the length of a character string,
including trailing blanks, whereas the LENGTH and LENGTHN functions return
the length of a character string, excluding trailing blanks. LENGTHC always
returns a value that is greater than or equal to the value of LENGTHN.
*
The LENGTHC function returns the length of a character string,
including trailing blanks, whereas the LENGTHM function returns the amount
of memory in bytes that is allocated for a character string. For fixed-
length character strings, LENGTHC and LENGTHM always return the same value.
For varying-length character strings, LENGTHC always returns a value that is
less than or equal to the value returned by LENGTHM.
----------------------------
Try to run below codes:
data S;
A='abcde12345';
X=substr(A, 1, 3);
Y=X||'XYZ';
Z=substr(A, 1, 3)|| 'XYZ';
L1=length(X);
L2=length(Y);
run;
You will find that:
Y='abc#######XYZ';
Z='abcXYZ#######';
# represents blank spaces.
The differences arise from the positions of these padded blanks, although they have same lengths. Then when you use LENGTH function, it will make a difference.. | o*******w 发帖数: 2310 | 7 Obs A X Y Z L1 L2
1 abcde12345 abc abc XYZ abcXYZ 13 6
Z have 6 bytes, no spaces at all.
always
【在 d******9 的大作中提到】 : Sorry, I was wrong. : The true reason is because of the below notes from SAS support: : Comparisons : * : The LENGTHC function returns the length of a character string, : including trailing blanks, whereas the LENGTH and LENGTHN functions return : the length of a character string, excluding trailing blanks. LENGTHC always : returns a value that is greater than or equal to the value of LENGTHN. : * : The LENGTHC function returns the length of a character string,
| o*******w 发帖数: 2310 | 8 So, how to explain the 13 in L2? where is the 7 blank spaces from?
data S;
A='abcde12345';
X=substr(A, 1, 3);
Y=X||'XYZ';
Z=substr(A, 1, 3)|| 'XYZ';
L1=length(y);
L2=lengthc(z);
run;
proc print;
run;
Obs A X Y Z L1
L2
1 abcde12345 abc abc XYZ abcXYZ 13 13 | d******9 发帖数: 404 | 9 Yes, this is the SAS output.
L2=6 because when you use LENGH function, it does not count the trailing
blanks in Z.
But for Y, the 7 blanks are in the middle of Y, so they really counts and L1
=13.
However, if you view the column properties, it will show that the lengths of
both Y and Z are both 13.
【在 o*******w 的大作中提到】 : So, how to explain the 13 in L2? where is the 7 blank spaces from? : data S; : A='abcde12345'; : X=substr(A, 1, 3); : Y=X||'XYZ'; : Z=substr(A, 1, 3)|| 'XYZ'; : L1=length(y); : L2=lengthc(z); : run; : proc print;
| o*******w 发帖数: 2310 | 10 Base SAS 47
The following SAS program is submitted:
data work.test;
First = 'Ipswich, England';
City_Country = substr(First,1,7)!!', '!!'England';
run;
Which one of the following is the length of the variable
CITY_COUNTRY in the output data set?
A. 6
B. 7
C. 17
D. 25
Base SAS 48
The following SAS program is submitted:
data work.test;
SAS Base Certification Page 16 of 83
http://sascert.blogspot.com/search?updated-min=2006-01-01T00:00 12/22/2011
22 comments Links to this post
Reactions:
First = 'Ipswich, England';
City = substr(First,1,7);
City_Country = City!!', '!!'England';
run;
Which one of the following is the value of the variable
CITY_COUNTRY in the output data set?
A. Ipswich!!
B. Ipswich, England
C. Ipswich, 'England'
D. Ipswich , England
【在 t*****2 的大作中提到】 : QUESTION 72 : The following SAS program is submitted: : data work.test; : First='Ipswich, England'; : City=substr(First,1,7); : City_Country=City ||', '||'England'; : run; : L1= length(City_Country); : L2= lengthc(City_Country); : 答案是: L1=25, L2=25
| | | o*******w 发帖数: 2310 | 11 Therefore, the correct answer is:
No matter what, the substr() result will have a data keep the original
variable's length by adding blank spaces before or after. | d******9 发帖数: 404 | 12 Yes. Exactly, this is the default !
But when you use LENGTH function to check the following concatenated
variables, the values may be different. Be careful !
【在 o*******w 的大作中提到】 : Therefore, the correct answer is: : No matter what, the substr() result will have a data keep the original : variable's length by adding blank spaces before or after.
| t*****2 发帖数: 94 | 13 没有解决我的问题呀。我知道长度怎么来的。问题的关键是:
City_Country=substr(First,1,7)||', '||'England';
7+2+7=16
但是LENGTHC给的答案是25呀。还有trim(City_Country)长度也是25.
我的问题是为什么lengthc是25?
疯掉了! | t*****2 发帖数: 94 | 14 没有解决我的问题呀。我知道长度怎么来的。问题的关键是:
City_Country=substr(First,1,7)||', '||'England';
7+2+7=16
但是LENGTHC给的答案是25呀。还有trim(City_Country)长度也是25.
我的问题是为什么lengthc是25?
LENGTH 给的是16.
length : 16
lengthc: 25
lengthc(trim(.)): 25
中间肯定是没有空格,结尾也没有呀。长度怎么可能是25呢?
疯掉了! | k*z 发帖数: 4704 | 15 because it is in the middle cant be deleted.
【在 t*****2 的大作中提到】 : 没有解决我的问题呀。我知道长度怎么来的。问题的关键是: : City_Country=substr(First,1,7)||', '||'England'; : 7+2+7=16 : 但是LENGTHC给的答案是25呀。还有trim(City_Country)长度也是25. : 我的问题是为什么lengthc是25? : LENGTH 给的是16. : length : 16 : lengthc: 25 : lengthc(trim(.)): 25 : 中间肯定是没有空格,结尾也没有呀。长度怎么可能是25呢?
| d******9 发帖数: 404 | 16 请仔细阅读我的回复。
In brief, 当空格在末尾的时候,length does not count the blanks, but lengthc
does!
say:
X=‘abcde###'; #= blank spaces.
Then the length of X is 8, which can be shown by viewing the column
properties, but when use length functions:
L1=length(X) = 5; --- does not count the trailing blanks.
L2=lengthc(X) =8; --- count the trailing blanks.
【在 t*****2 的大作中提到】 : 没有解决我的问题呀。我知道长度怎么来的。问题的关键是: : City_Country=substr(First,1,7)||', '||'England'; : 7+2+7=16 : 但是LENGTHC给的答案是25呀。还有trim(City_Country)长度也是25. : 我的问题是为什么lengthc是25? : LENGTH 给的是16. : length : 16 : lengthc: 25 : lengthc(trim(.)): 25 : 中间肯定是没有空格,结尾也没有呀。长度怎么可能是25呢?
|
|