BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
prehendere
Calcite | Level 5

 

Hello,

 

I'm preparing the sas base certification but there is a point that I don't understand

 

The question is to give the length of City_Country after the following program is submitted:

 

data work.test;
First = 'Ipswich, England';
City_Country = substr(First,1,7)!!', '!!'England';
run;

 

Apparently the answer is 25, but why is it not 16? (Ipswich+, +England), is there a mistake in the solution?

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

As the documentation of the SUBSTR Function says:

 

"In a DATA step, if the SUBSTR (right of 😃 function returns a value to a variable that has not previously been assigned a length, then that variable is given the length of the first argument."

 

Meaning that substr(First,1,7) has the length 16, ', ' has the length 2 and 'England' has the length 7 giving the variable City_Country a total length of 25. 

View solution in original post

3 REPLIES 3
art297
Opal | Level 21

From the documentation ( https://support.sas.com/documentation/cdl/en/lrcon/62955/HTML/default/viewer.htm#a000780367.htm 😞

The concatenation operator (||) concatenates character values.
The results of a concatenation operation are usually stored in
a variable with an assignment statement, as in 
level='grade '||'A' . The length of the resulting variable is
the sum of the lengths of each variable or constant in the
concatenation operation, unless you use a LENGTH or ATTRIB
statement to specify a different length for the new variable.

Thus, the length of first (16) + the lengths of ', ' and 'England'

 

Art, CEO, AnalystFinder.com

 

 

 

 

 

 

PeterClemmensen
Tourmaline | Level 20

As the documentation of the SUBSTR Function says:

 

"In a DATA step, if the SUBSTR (right of 😃 function returns a value to a variable that has not previously been assigned a length, then that variable is given the length of the first argument."

 

Meaning that substr(First,1,7) has the length 16, ', ' has the length 2 and 'England' has the length 7 giving the variable City_Country a total length of 25. 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 2762 views
  • 1 like
  • 3 in conversation