Skip to content
Advertisement

Calculation based on variable containing multiple possible substrings

I have 2 variables (source and platform) that has results such as:

“Source”: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36 Edg/100.0.1185.44 etc.

“Platform”: Win32 etc.

I want to create 2 new variables (device and system), “extracted” from the above ones.

I found some code, but I don’t know how to implement it in spss syntax (or even if it’s possible).


const getDeviceType = () => {
  const ua = navigator.userAgent;
  if (/(tablet|ipad|playbook|silk)|(android(?!.*mobi))/i.test(ua)) {
    return "tablet";
  }
  if (
    /Mobile|iP(hone|od)|Android|BlackBerry|IEMobile|Kindle|Silk-Accelerated|(hpw|web)OS|Opera M(obi|ini)/.test(
      ua
    )
  ) {
    return "mobile";
 }
  return "desktop";
};

function getOS() {
  var userAgent = window.navigator.userAgent,
      platform = window.navigator.platform,
      macosPlatforms = ['Macintosh', 'MacIntel', 'MacPPC', 'Mac68K'],
      windowsPlatforms = ['Win32', 'Win64', 'Windows', 'WinCE'],
      iosPlatforms = ['iPhone', 'iPad', 'iPod'],
      os = null;

  if (macosPlatforms.indexOf(platform) !== -1) {
    os = 'Mac OS';
  } else if (iosPlatforms.indexOf(platform) !== -1) {
    os = 'iOS';
  } else if (windowsPlatforms.indexOf(platform) !== -1) {
    os = 'Windows';
  } else if (/Android/.test(userAgent)) {
    os = 'Android';
  } else if (!os && /Linux/.test(platform)) {
    os = 'Linux';
  }

  return os;
}

I’ve also tried this, but every variables turns “zero”:

IF PLATFORM contains the word 'Macintosh' or  'MacIntel' or 'MacPPC' or 'Mac68K'
Then SYSTEM is “Mac OS”
Else if PLATFORM contains the word 'iPhone' or 'iPad' or 'iPod' 
Then SYSTEM is “iOS”
Else if PLATFORM contains the word 'Win32' or 'Win64' or 'Windows' or 'WinCE'
Then SYSTEM is “Windows”
Else if PLATFORM contains the word 'Android'
Then SYSTEM is “Android”
Else if PLATFORM does not contains ‘os’ but contains the word ‘Linux’ 
Then SYSTEM is “Linux”

and

IF SOURCE contains words tablet or ipad or playbook or silk or (android but not mobi)
Then DEVICE = Tablet
Else If source contains Mobile|iP(hone|od)|Android|BlackBerry|IEMobile|Kindle|Silk-Accelerated|(hpw|web)OS|Opera M(obi|ini)
Then DEVICE = mobile
Else DEVICE = desktop

and

COMPUTE test = CHAR.INDEX(platform,"windows").
EXECUTE.
string c (A255).
IF test>0 c="windows".
EXECUTE.

COMPUTE wind = CHAR.INDEX(source,"windows").
EXECUTE.
COMPUTE wind32 = CHAR.INDEX(source,"Win32").
EXECUTE.
STRING c2 (A255).
IF wind>0 or wind32 > 0 c="Windows".
EXECUTE.

Can anyone help me on this? Thanks!

Advertisement

Answer

Instead of having all the values in the syntax itself I suggest a different strategy: you create a table of possible substrings in the source column and the corresponding value in the target column – and save it in a separate file, in which you can make corrections and additions. So for example:

sourceVal | targetVal 
----------|----------
  windows | Windows  
  Windows | Windows  
    win32 | Windows  
   win 10 | Windows  
  Android | Android
  android | Android

Once you use this as a dictionnary with the following syntax:

(dataset dict has the dictionary you’ve created, dataset orig is the original data you’re working on)

dataset activate dict.
string cmd (a200).
compute cmd=concat("if char.index(source, '", rtrim(sourceVal), "')>0 device='", targetVal, "'.").
write out="c:yourpathrunthis.sps" /cmd.
exe.

At this point you’ve created a new syntax file which has a separate command for each substring identified in your dictionnary. Now you can run it on the original data:

dataset activate orig.
string device (a200).
insert file="c:yourpathrunthis.sps".
exe.

If you want the same command to search both in source and platform, the simplest way is to create a new variable containing both of them:

string srcPlat (a2000)
compute srcPlat=concat(rtrim(source), rtrim(platform))

now update the cmd accordingly.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement