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
Click Comment link to get answer
can i have the explanaion?
ReplyDeleteuse this:
ReplyDeletedata work.test;
First = 'Ipswich, England';
City_Country = substr(First,1,7)!!', '!!'England';
yy=length(City_Country);
run;
yy= 16(7+2+7)
data work.test;
ReplyDeleteFirst = 'Ipswich, England';
City_Country = substr(First,1,7)!!', '!!'England';
run;
proc contents data=test;
run;
This is the result of properties and length of variables
Alphabetic List of Variables and Attributes
# Variable Type Len
2 City_Country Char 25
1 First Char 16
By default the variable length is determined by the length of the first argument for substr.
ReplyDeleteSo here we have 16 + 2(', ') + 7('England') = 25
thank u
Deletemysasworld,
ReplyDeleteuse VLENGTH rather than LENGTH
I did not recall using these !!. I ran the code so I got the answer. So !! work like ||?
ReplyDeleteYes you're right, !!can be treat exactly like ||
DeleteYes !! and || are same, some OS machines don't have pipe character for that reason SAS made esclamation mark same as pipe.
ReplyDeleteThanks for clearing the confusion
DeleteC
ReplyDeleteGh4.26.11 please if you disagree,let us know your logic.
ReplyDeleteIf we run the data , we get the answer which is 25 character but what is the logic?
What if we add Cophegn in stead of added word England.
Just trying to elaborate Sudha.
First variable has 16 length, asked to substract 7 letter that would be Enland and if we add Cophegn 7 letter word.
The 'First" variable still occupies 16 length then a space and , and 7 letter 'Cophegn' added makes total 25.
The word England taken out and added back makes little confuse.
Is it any thing to do with the variable in inverted comma 'Ipswich, England' ? It takes the letter or character out but it does not trim the length. Without the trim it would take all the space in variable "first" plus space , and 7 letter= 25.
Did I clear it?
Sakar Sham
"In SAS programming, the concatenation operator is a pair of vertical bars (||). If your keyboard does not have a solid vertical bar, use two broken vertical bars (¦¦) or two exclamation points (!!).
ReplyDeleteThe length of the new variable is the sum of the lengths of the pieces or number of characters that is specified in a LENGTH statement for the new variable."
(ref - Sas help and documentation)
Answer is 25 because :
length of First = 16
length of ', ' = 2
length of 'England' = 7
Total length = 25. Thus Answer = D
has anyone given it a thought what would the output be if one tried to print len=length(City_Country) --> 15
ReplyDeleteThe confusion in this case is that there is a string length and a variable length. This question specifically asks about the variable length.
ReplyDeletedata work.BaseSAS47;
First = 'Ipswich, England';
City_Country = substr(First,1,7)!!', '!!'England';
leng = length(City_Country);
vleng = vlength(City_Country);
run;
string length = 16
variable length = 26
typo; variable length = 25
Deletethe code is giving the following output
ReplyDeleteFirst City_Country
Ipswich, England Ipswich, England
If the answer is D 25; is it not supposed to give
Ipswich, England, England (tot character length 25)
Ans:D
DeleteTry the code snippet:
data work.test;
First = 'Ipswich, England';
City_Country = subst(First,1,7)!!', '!!'England';
run;
proc contents data=test;
run;
Why is the length of First = 16? Shouldn't it be 7?
ReplyDeleteI thought the same thing. Because the substring fn says (First, 1, 7) meaning length 7 from position 1.
Deletecoz FIRST='Ipswich, England'=16
Delete25 is the correct answer.
ReplyDeleteSAS reserves the same length as the variable it is substr'ing on or 16 positions. you then add 2 positions for the ', ' and then you add 7 positions for 'England' for a total of 25.
There's another question coming up dealing with Substr(Scan and scan reserves 200 positions. That's what I was getting confused on.
This is hard to believe but the correct answer is D.
ReplyDeleteThe compiler is stupid or lazy to set the length of City_Country as vlength(First)+vlength(', ')+vlength('England') although the value of City_country is 'Ipswich, England'.
what is !! function in sas ?
ReplyDeleteThis is from SAS prep guide. So a space and a comma is default in scan an will always be taken into account
ReplyDeleteTo create the LastName variable to store the employee's last name, you write an assignment
statement that contains the following SCAN function:
LastName=scan(name,1,' ,');
Note that a blank and a comma are specified as delimiters. You can also write the function
without listing delimiters, because the blank and comma are default delimiters.
LastName=scan(name,1);
D
ReplyDeletelength of First = 16
ReplyDeletelength of ', ' = 2
length of 'England' = 7
=25
D