Search code examples
sasfinance

Modifying a list of ticker symbols in sas by deleting the last few characters


I have a long list of time-series price data sorted by ticker symbol and then by date. I'm looking to delete the last four characters on every ticker. Say the ticker is AAA.ASX, I would like to end up with just AAA to match and merge with another data set.

So far I've tried to code:

asx=tranwrd(asx,".asx",""); put asx; run;

Any advice would be appreciated as I'm just getting into this whole sas thing.


Solution

  • To expand upon Joe's answer slightly, scan includes . as one of it's standard delimiters and so the third argument is not strictly necessary (but it's good practice to be explicit).

    data tmp;
        asx = "abc.def";
    
        /* Return the first word, when seperated by . */ 
        asx1 = scan(asx, 1, ".");
        put asx1=;
    
        /* Return the first word, when seperated by standard delimiters _ -,."' etc. */ 
        asx2 = scan(asx, 1);
        put asx2=;
    
        /* Return only the first 3 characters (if you know it will always be 3 long) */
        asx3 = substr(asx, 1, 3);
        put asx3=;
    
        /* Return the substring from position 1 to the first occourance of . */
        asx4 = substr(asx, 1, index(asx, ".") - 1);
        put asx4=;
    run;
    

    As your learning SAS it may also be worth your while to check out some of the other string manipulation functions like substr and index as it's great to have multiple tools available.