Tips & Tricks #27 – convert num to string and uses concat
I got a question from my French colleague and the question was how to convert these 2 numeric variables to string variables and then put them together to a new stringvariable with the CONCAT command.
Here is the example of the data file:
First go into the “Variable View” and change the format of the 2 numeric variables. Note what the maximum number of characters each variable has:
Click on the blue box at the “Type” column (see Picture):
As the variable ENQ has just 1 character, then convert this variable like this:
Then do almost the same with the other variable MEN, but convert it to STRING with 4 characters in this example because the maximum number of characters is 4.
Then choose the command Transform – Compute Variable and do the LTRIM command (left trimming) of the last variable, so we don’t have any empty spaces in the beginning of the string. See below:
You also have to click the “Type & Label” button on this new variable and choose the format as STRING (See below):
The result of the last variable MEN will then look like this and you can now see that the left part is trimmed down:
Now it’s time to put ENQ and Ltrim_MEN together with the CONCAT-command. Choose command Transform – Compute again, and click on “Reset” to start from the beginning in the dialog box:
Also here you have to click the “Type & Label“-button, and change format to STRING with 5 characters.
And here is the result:
If you should use the variable MEN instead of Ltrim_MEN the new variable NUMQUEST would get other strings:
1 123, 33245, 2 222, 4 313, 44531, 1 555, 22313, 3 34, 33456, 41234 – so in that case you sometimes will get empty spaces.
Greetings Gunilla Rudander