Home Forums Main Forums SAS Forum The weird but useful NOTNAME() function in SAS

  • The weird but useful NOTNAME() function in SAS

     Justin updated 7 months, 1 week ago 1 Member · 1 Post
  • Justin

    Administrator
    February 7, 2021 at 5:38 pm

    NOTNAME Function
    This function searches a character string for an invalid character in a SAS variable name
    under VALIDVARNAME=V7, and returns the first position at which that character is found.

    Comparisons
    The NOTNAME function searches a string for the first occurrence of any character that is not valid in a SAS variable name under VALIDVARNAME=V7. The ANYNAME function searches a string for the first occurrence of any character that is valid in a SAS variable name under VALIDVARNAME=V7.

    Examples
    The following example uses the NOTNAME function to search a string for any character that is not valid in a SAS variable name under VALIDVARNAME=V7.

     data _null_;    
    string='Next = _n_ + 12E3;';
    j=0;
    do until(j=0);
    j=notname(string,j+1);
    if j=0 then put +3 "That's all";
    else do;
    c=substr(string,j,1);
    put +3 j= c=;
    end;
    end;
    run;

    The following lines are written to the SAS log:
    j=5 c=
    j=6 c==
    j=7 c=
    j=11 c=
    j=12 c=+
    j=13 c=
    j=18 c=;
    That’s all

     data _null_;    
    string='Next = _n_ + 12E3;';
    j=0;
    do until(j=0);
    j=anyname(string,j+1);
    if j=0 then put +3 "That's all";
    else do;
    c=substr(string,j,1);
    put +3 j= c=;
    end;
    end;
    run;

    The following lines are written to the SAS log:
    j=1 c=N
    j=2 c=e
    j=3 c=x
    j=4 c=t
    j=8 c=_
    j=9 c=n
    j=10 c=_
    j=14 c=1
    j=15 c=2
    j=16 c=E
    j=17 c=3
    That’s all

    Isn’t it cool? Try it in SAS!

Log in to reply.

Original Post
0 of 0 posts June 2018
Now