{"id":263,"date":"2013-09-07T06:53:15","date_gmt":"2013-09-07T06:53:15","guid":{"rendered":"http:\/\/localhost\/spss-tutorials\/?p=263"},"modified":"2016-05-23T06:38:59","modified_gmt":"2016-05-23T06:38:59","slug":"spss-string-variables-basics","status":"publish","type":"post","link":"https:\/\/www.spss-tutorials.com\/spss-string-variables-basics\/","title":{"rendered":"SPSS String Variables Basics"},"content":{"rendered":"<!--body-->\n\n<p>For <strong>working proficiently with SPSS string variables<\/strong> , it greatly helps to understand some string basics. This tutorial explains what SPSS string variables are and demonstrates their main properties.<br>\nWe encourage you along by downloading and opening <a href=\"https:\/\/www.spss-tutorials.com\/downloads\/string_basics.sav\">string_basics.sav<\/a>. The syntax we use can be copy-pasted or downloaded <a href='https:\/\/spss-tutorials.com\/downloads\/spss-string-variables-basics.sps'>here<\/a>.\n<\/p>\n\n<span class='img w600'>\n\t<img src='https:\/\/spss-tutorials.com\/img\/spss-string-variables-basics-data-file.png' alt='SPSS String Variable Basics Data File'>\n<\/span>\n\n\n<h2>SPSS String Variables - What Are They?<\/h2>\n\n<p>String variables are one of SPSS' two <a href=\"https:\/\/www.spss-tutorials.com\/spss-variable-types-and-formats\/ \">variable types<\/a>. What <em>really<\/em> defines a string variable is the way its values are stored internally.<span class='inline-comment'>We won't go into this technical matter here but those who really want to know may consult our <a href=\"https:\/\/unicode-table.com\/en\/\">Unicode<\/a> tutorial.<\/span> A simpler definition is that string variables are <strong>variables that hold zero or more text characters<\/strong>.<br>\nString values are always treated as text, even if they contain only numbers. Some surprising consequences of this are shown towards the end of this tutorial.<\/p>\n\n<h2 id='a-format'>SPSS String Format<\/h2>\n\n<p>String variables in SPSS usually have an <strong>&ldquo;A&rdquo; format<\/strong>, where &ldquo;A&rdquo; denotes &ldquo;Alphanumeric&rdquo;. This can be seen by running the following line of <a href=\"https:\/\/www.spss-tutorials.com\/spss-syntax\/ \">syntax<\/a>\n<span class='code'>display dictionary.<\/span>\nafter opening the data. The result, shown in the screenshot below, confirms that we have two string variables having A3 and A8 formats.<\/p>\n\n<span class='img w840'>\n    <img src='https:\/\/spss-tutorials.com\/img\/spss-string-formats-in-output.png' alt='SPSS String Variable Formats'>\n<\/span>\n\n<p>The numeric suffixes (3 and 8 here) are the numbers of <strong>bytes<\/strong> that the values can hold. Starting from SPSS version 16, some characters may consist of two bytes.<span class='inline-comment'>This is explained in Unicode mode.<\/span> If you don't want to go into details, just choose string lengths that are <strong>twice the number of characters<\/strong> they need to contain to stay on the safe side.<\/p>\n\n\n\n<h2 id='string-command'>SPSS String Command<\/h2>\n\n<p>Commands that pass values into variables, most notably <code>COMPUTE<\/code> and <a href=\"https:\/\/www.spss-tutorials.com\/spss-if-command\/ \">IF<\/a>, can be used for both existing and new numeric variables. However, they can't be used for new string variables; you must <strong>first create one or more new, empty string variables<\/strong> before you can pass values into them. This is done with the <code>STRING<\/code> command. Its most basic use is\n<span class='code'>STRING variable_names (A10).<\/span>\nAs explained earlier, A10 means that the new variable can hold values of up to 10 bytes. The syntax below creates a new string variable in our test data.<\/p>\n\n<div class='code'><strong>*1. Create empty new string variable with string command.<br><\/strong><br>string string_3(a10).<br><br><strong>*2. Pass values into new string variable.<br><\/strong><br>compute string_3 = &#39;Hello&#39;.<br>exe.<\/div><!--class='code'-->\n\n\n<h2 id='string-function'>SPSS String Function<\/h2>\n\n<p>SPSS' string function <strong>converts numeric values to string values<\/strong>. Its most basic use is\n<span class='code'>compute s2 = string(s1,f1).<\/span>\nwhere s2 is a string variable, s1 is a numeric variable or value and f1 is the numeric <a href=\"https:\/\/www.spss-tutorials.com\/spss-variable-types-and-formats\/#variable-formats\">format<\/a> to be used.<br>\nWith regard to our test data, the syntax below shows how to convert numeric_1 into (previously created) string_3. In order to capture all three digits, we need to specify f3 as the format.<\/p>\n\n<div class='code'><strong>*Convert numeric_1 to (existing) string variable with string function.<br><\/strong><br>compute string_3 = string(numeric_1,f3).<br>exe.<\/div><!--class='code'-->\n\n<!-- <h2 id='number-function'>SPSS NUMBER Function<\/h2> -->\n\n\n\n<h2>Quotes Around String Values<\/h2>\n\n<p>If you use string values in syntax, put quotes around them. For example, say we want to flag all cases whose name is &ldquo;Stefan&rdquo;. The screenshot shows the desired result. The syntax below demonstrates the wrong way and then the right way to do so.<span class='inline-comment'>A faster way to do this is <code>compute find_stefan = string_2 = 'Stefan'.<\/code> <a href=\"https:\/\/www.spss-tutorials.com\/compute-a-is-b-is-c\/\">Compute A = B = C<\/a> explains how this works.<\/span><\/p>\n\n<div class='code' style='width:30em;'><strong>*1. Compute empty flag variable.<br><\/strong><br>compute find_stefan = 0.<br>exe.<br><br><strong>*2. Wrong way: without quotes Stefan is thought to be variable name.<br><\/strong><br>if string_2 = Stefan find_stefan = 1.<br>exe.<br><br><strong>*3. Right way: quotes around Stefan.<br><\/strong><br>if string_2 = &#39;Stefan&#39; find_stefan = 1.<br>exe.<\/div><!--class='code'-->\n<h2>Result<\/h2>\n\n<span class='img w480'>\n\t<img src='https:\/\/spss-tutorials.com\/img\/spss-string-variable-flag-cases.png' alt='SPSS String Variable Flag Cases'>\n\t<span class='small'>Flagging Cases Whose Name is Stefan<\/span>\n<\/span>\n\n<p>Note that the second step triggers SPSS error #4285: due to the omitted quotes, SPSS thinks that Stefan refers to a variable name and doesn't find it in the data.<\/p>\n\n\n\n<h2>String Values are Case Sensitive<\/h2>\n\n<p>Now let's create a similar flag variable for cases called &ldquo;Chrissy&rdquo;. After running step 2 in the syntax below, you can see in <a href=\"https:\/\/www.spss-tutorials.com\/spss-data-editor-window\/#spss-data-view\">data view<\/a> that no cases have been flagged; it uses the <strong>wrong casing<\/strong>. Step 3, using the correct casing, <em>does<\/em> flag &ldquo;Chrissy&rdquo; correctly.<\/p>\n\n<div class='code'><strong>*1. Compute empty flag variable.<br><\/strong><br>compute find_chrissy = 0.<br>exe.<br><br><strong>*2. Line below doesn&#39;t flag any cases because &#39;chrissy&#39; is not the same as &#39;Chrissy&#39;.<br><\/strong><br>if string_2 = &#39;chrissy&#39; find_chrissy = 1.<br>exe.<br><br><strong>*3. Right way: &#39;Chrissy&#39; instead of &#39;chrissy&#39;.<br><\/strong><br>if string_2 = &#39;Chrissy&#39; find_chrissy = 1.<br>exe.<\/div><!--class='code'-->\n\n<h2 id='system-missing'>SPSS String Variables - System Missing Values<\/h2>\n\n<p>There's no such thing as a <a href=\"https:\/\/www.spss-tutorials.com\/spss-missing-values-tutorial\/#sysmis\">system missing value<\/a> in a string variable; string values consisting of zero characters which are called <strong>empty strings are valid values in SPSS<\/strong>.<span class='inline-comment'>Also note that you don't see a dot (indicating a system missing value) in an empty cell of a string variable.<\/span> We can confirm this by running <a href=\"https:\/\/www.spss-tutorials.com\/spss-frequencies-command\/\">FREQUENCIES<\/a>:\n<span class='code'>frequencies string_2.<\/span>\nNote that the empty string value is among the valid values.<\/p>\n\n<h2>Result<\/h2>\n\n<span class='img w480'><img src='https:\/\/spss-tutorials.com\/img\/spss-string-variable-no-system-missing-values.png' alt='SPSS String Variable No System Missing Values'><\/span>\n\n<h2 id='user-missing'>User Missing Values in String Variables<\/h2>\n\n<p>\nOver the years, we've seen many forum questions (and some heated debates) regarding <a href=\"https:\/\/www.spss-tutorials.com\/spss-missing-values\/#spss-user-missing-values\">user missing values<\/a> in string variables. Well, running\n<span class='code'>missing values string_2('').<\/span>\nspecifies the empty string as a user missing value. This can be confirmed by rerunning its frequency table; the empty string is now in the missing values section as shown by the screenshot.<\/p>\n\n<h2>Result<\/h2>\n\n<span class='img w480'><img src='https:\/\/spss-tutorials.com\/img\/spss-string-variable-user-missing-values.png' alt='SPSS String Variable No System Missing Values'><\/span>\n\n<h2 id='sorting'>Sorting on String Variables<\/h2>\n\n<p><strong>String values are seen as text<\/strong>, even if they consist of only numbers. A consequence is that string values are sorted alphabetically<strong><\/strong>. To see what this means, run\n<span class='code'>sort cases by string_1.<\/span><\/p>\n\n\n<span class='img w360'>\n\t<img src='https:\/\/spss-tutorials.com\/img\/spss-string-variable-sorted-alphabetically.png' alt='SPSS String Variable Sorted Alphabetically'>\n\t<span class='small'>Alphabetical Sorting of string_1<\/span>\n<\/span>\n\n<p>If this result puzzles you, represent the numbers 0 through 9 by letters a through j. Clearly, &ldquo;bb&rdquo; (= 11) comes <em>before<\/em> &ldquo;c&rdquo; (= 2) if sorted alphabetically.<\/p>\n\n<h2 id='calculations'>No Calculations on String Variables<\/h2>\n\n<p>Because string values are seen as text, you can't do any calculations on them. For instance a <a href=\"https:\/\/www.spss-tutorials.com\/spss-compute-command\/\">COMPUTE<\/a> command with some <a href=\"https:\/\/www.spss-tutorials.com\/spss-main-numeric-functions\/\">numeric function<\/a> like\n<span class='code'>compute string_1 = string_1 * 2.<\/span>\nwill trigger SPSS error #4307. It basically tries to tell us that our command crashed because a string variable was used in a calculation.\n<\/p>\n\n<span class='img w840 smt smb'><img src='https:\/\/spss-tutorials.com\/img\/spss-error-4307.png' alt='SPSS Error #4307'><\/span>\n\n<p>In a similar vein, most procedures involve calculations and thus won't run on string variables either. For example,\n<span class='code'>descriptives string_1.<\/span>\nwon't produce any other results than a warning that the command crashed because only string variables were involved.<\/p>\n\n\t\n","protected":false},"excerpt":{"rendered":"<p>String variables are variables that hold zero or more text characters. This tutorial demonstrates their main properties by running simple examples on a test data file.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[68],"tags":[],"class_list":["post-263","post","type-post","status-publish","format-standard","hentry","category-spss-string-variables"],"_links":{"self":[{"href":"https:\/\/www.spss-tutorials.com\/wp-json\/wp\/v2\/posts\/263","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.spss-tutorials.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.spss-tutorials.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.spss-tutorials.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.spss-tutorials.com\/wp-json\/wp\/v2\/comments?post=263"}],"version-history":[{"count":0,"href":"https:\/\/www.spss-tutorials.com\/wp-json\/wp\/v2\/posts\/263\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.spss-tutorials.com\/wp-json\/wp\/v2\/media?parent=263"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.spss-tutorials.com\/wp-json\/wp\/v2\/categories?post=263"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.spss-tutorials.com\/wp-json\/wp\/v2\/tags?post=263"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}