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

Hi all SAS Users,

 

Today I faced this question and I still cannot find out why I get it wrongly

Phil_NZ_0-1617523042918.png

To me, there are two correct answers for this question. I cannot find the reason why the option a is wrong. CATS just simply concatenate all the word together without trimming or else and I think it is right in this case.

 

Warmest regards,

Thank you for your help, have a fabulous and productive day! I am a novice today, but someday when I accumulate enough knowledge, I can help others in my capacity.
1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

Option a) using cat() - not cats() - concatenates without any trimming as you write and for this reason would leave all the trailing blanks in City untouched. You would end up with a string like: Athens         , Greece

View solution in original post

4 REPLIES 4
Patrick
Opal | Level 21

Option a) using cat() - not cats() - concatenates without any trimming as you write and for this reason would leave all the trailing blanks in City untouched. You would end up with a string like: Athens         , Greece

ballardw
Super User

Real simple to TRY the actual code.

data test;
   length city $ 9 country $ 11;
   input city country;
   citycountry= cat(city,',  ',country);
datalines;
Athens Greece
;

The lengths for the variables were set using the longest value shown for any of the displayed values.

Since pictures are involved can't really tell whether the comma and spaces includes two or three spaces but it should be somewhat obvious with the code above.

Phil_NZ
Barite | Level 11

Hi @ballardw 

Thank you for your explanation, but there is one point I am confusing if I get you wrongly.


@ballardw wrote:
The lengths for the variables were set using the longest value shown for any of the displayed values.

I think if you do not set up the length statement, SAS will recognize the length for an observation made from execution in different ways, not necessarily the longest value.

For example, if you import the data from excel without any length assigned, it will read a couple of first rows to assign the length for this variable, or you can modify the number of rows read by using guessingrow=value.

 

Second, if you use the if..then statement to create a variable, it will assign the length of this variable by the length of the first observation being read as the length of the new variable.

 

Third, if you use the calculation statement, the length of the new variable will follow the length of the original variable.

Fourth, when you concatenate the datasets, the attributes of one variable will follow the attribute of this variable in the first dataset.

 

Please let me know if I fall into any fallacy.

 

Warm regards and thanks.

 

 

Thank you for your help, have a fabulous and productive day! I am a novice today, but someday when I accumulate enough knowledge, I can help others in my capacity.
ballardw
Super User

I, as in ME, wrote a data step given the information available. If I did not do that the default length if I used:

input city $ country $;

would default to 8 characters. 8 character lengths would behave differently. I manually counted the number characters displayed in the picture to set the length because there was no other information provided but the lengths I chose were the absolute minimum that would hold the shown values of City and Country.

If you test code by doing something like City='Athens'; then the length is 6 characters and behaves differently, as in wouldn't show the runtime result error that the real data would.

 

SAS has lots of rules setting lengths of variables. NONE of which was provided in the question description. I was providing an example of writing a data step to test the different proposed solutions with just one record to demonstrate why that one possible solution was incorrect so you would have a chance to write one yourself later and test code instead of asking questions.

 

Import, by which you mean proc import is poor pattern to follow. Why you may ask. Depending on the data source the default number of rows examined to set variable properties is very small and may result in values too short to hold all values.

One reason why I write data steps to read text files so I can control and KNOW the lengths and types of variables.

 

Not just "if then", almost any statement that has a value can set the length. The first use is what does that, not the statement type.

For instance a DO loop can create a variable

data junk;
   do i='word', 'johnny', 'something longer';
     output;
   end;
run;

Guess what the output looks like. Then run the code and examine the data set to verify.

 

Third, if you use the calculation statement, the length of the new variable will follow the length of the original variable.

I strongly suggest that you check a large number of the character functions.

In a large number of cases the length of the returned variable for previously undefined variables is actually 200. In some cases if multiple variables are involved then the length is the sum of the lengths.

Consider:

data junk;
   do i='word', 'johnny', 'something longer';
     j= cats(i,' to the ball game');
     output;
   end;
run;

how long will the variable j be? Assuming i is 4 characters you might think j will be 4 + 16 (the number of characters in the quoted text). Run the code. Run proc contents on it. J has length of 200.

SAS does some of this to partially protect you from poor coding when you don't set a proper length. The use of 200 is the former maximum length of a character variable, so SAS defaulted to "we weren't told how long results might be so assume the worst and use 200, it should fit". So if the first use of the variable is the result of any of these functions you get much larger than often expected results.

And then you may use another function, as in this question, that uses the value and keeps all of those trailing blanks as part of the value.

 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 4 replies
  • 433 views
  • 3 likes
  • 3 in conversation