EZ Study

Actuarial Biology Chemistry Economics Calculators Confucius Engineer

SAS Tip: Read external files from FTP into SAS
Difference between Symput and Symputx

Question: How can I read external files from FTP into SAS?
Answer: The following code is to get an external csv file from some ftp into SAS

        FILENAME ftpname FTP "test.csv" LRECL=280
        PASS='yourpassword' ;

        /********** use Infile statement to read data **************/
        data WORK.test;
        %let _EFIERR_ = 0;
        /*define a macro variable to keep track of ERROR status*/
        infile ftpname delimiter =','

        MISSOVER DSD lrecl=32767 firstobs=1;
        informat var1 $15.;
        informat var2 $20.;
        format var1 $15. ;
        format var2 $20. ;
        /* For a variable, informat tells it how to read data */
        /* format tells it how to write the value out. */
        input var1 $ var2 $;
        if _ERROR_ then call symputx('_EFIERR_',1);
        /* _ERROR_ is a SAS system variable.*/
        /* If no error, _ERROR_=0; if there is any error, _ERROR_=1.         */
        /* Assign the value of _ERROR_ to a macro variable named EFIERR */
        /* The following part explains symputx with more details.              */

Question: What is the difference between symputx and symput?
Answer: In short, Call symputx is more convenient/better than Call Symput;
The following code shows how symputx works bettern than symput:
        data _null_;
        call symputx(' symputx_item     ', '     leading and trailing blanks removed');
        call symput('symput_item', '     leading and trailing blanks removed ');
        call symputx('     x     ', 123.456, 'g'); /* "g":global, "l": local */
        run; /*put some blanks on purpose */
        %put symput_item=!&symput_item!;
        %put symputx_item=!&symputx_item!;
        %put x=!&x!; /* use "!" to help identify no blanks. */

In the SAS log output, you can see:

        symput_item=!     leading and trailing blanks removed!
        symputx_item=!leading and trailing blanks removed!

  There is no such convenience in call symput, as you can try by yourself by changing the above code to symput, you need to use trim or strip or left functions to get rid of blanks. SAS introduced CALL SYMPUTX in version 9 to address the pitfalls of CALL SYMPUT. CALL SYMPUTX is similar to CALL SYMPUT except that,

  CALL SYMPUTX does not write a note to the SAS log when the second argument is numeric. CALL SYMPUT, however, writes a note to the log stating that numeric values were converted to character values.

  CALL SYMPUTX uses a field width of up to 32 characters when it converts a numeric second argument to a character value. CALL SYMPUT uses a field width of up to 12 characters.

Here is another example to indicate this:
        data _null_; x=1234;
        call symput("symput_var",x);
        call symputx("symputx_var",x); run;

        %put symput_var: &symput_var;
        %put symputx_var: &symputx_var;

Output in the log window:
        symput_var:                 1234
        symputx_var: 1234
Note that there are 8 empty spaces before 1234 in the macro varable: symput_var

  CALL SYMPUTX left-justifies both arguments and trims trailing blanks. CALL SYMPUT does not left-justify the arguments, and trims trailing blanks from the first argument only. Leading blanks in the value of name cause an error.

Related links:

Continue to next: Tips for survey data: Remove carriage returns   SAS tutorial home
Back to: How to read multiple files into SAS within one step?   Statistics tutorial home