Posted By

rambles on 09/13/11


Tagged

sas DataManagement


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

csrsen


Text to SAS Variable Name


 / Published in: SAS
 

Useful functions to ensure (99% of the time...) that a piece of text can be used to name a SAS variable. NOTE: This won't work with SAS literals.

  1. data _null_;
  2. name="1. Long text with no. at start - and some double spaces";
  3.  
  4. name = compress(strip(name), " ", 'kn'); ** Keep numbers, underscore character and letters (A - Z, a - z);
  5. name = translate(compbl(strip(name)),'_'," "); ** Convert dupe blanks to single and replace blanks with underscore;
  6. name = substrn(strip(name), 1, 31); ** Keep the first 31 chars (var name limit is 32);
  7. if input(substr(strip(name),1,1), ?? 3.) then name = "_"||name; ** Add "_" to names that start with a number;
  8. put name=;
  9.  
  10. run;

Report this snippet  

You need to login to post a comment.