Text to SAS Variable Name


/ Published in: SAS
Save to your folder(s)

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.


Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.