Created
June 15, 2018 02:38
-
-
Save statgeek/a2614d336d59f9e0af716c04f2177af0 to your computer and use it in GitHub Desktop.
SAS - check for the existence of specific variables in a data step
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /*This program checks if two variables exist in a data step and then does conditional logic | |
| if the variables are present | |
| This is also an example of CALL VNEXT()*/ | |
| /*Generate sample data*/ | |
| *both variables exist; | |
| data class1; | |
| set sashelp.class; | |
| site='1'; | |
| A1_loc='2'; | |
| drop name; | |
| run; | |
| *one variable exists; | |
| data class2; | |
| set sashelp.class; | |
| site='1'; | |
| drop name; | |
| run; | |
| *neither variable exists; | |
| data class3; | |
| set sashelp.class; | |
| drop name; | |
| run; | |
| *Macro to check using CALL VNEXT; | |
| %macro check_var_exist(dsn=); | |
| data test; | |
| set &dsn end=eof; | |
| length name $32 type $3; | |
| name=' '; | |
| length=666; | |
| var1=0; | |
| var2=0; | |
| if _n_ = 1 then do; | |
| do i=1 to 99 until(name=' '); | |
| call vnext(name, type, length); | |
| if upcase(name)='A1_LOC' then | |
| var1=1; | |
| if upcase(name)='SITE' then | |
| var2=1; | |
| end;*end of do loop; | |
| if var1 and var2 then | |
| put "&dsn: Both variables found"; | |
| else | |
| put "&dsn: Not found"; | |
| end;*end of first record; | |
| *remaining data step code; | |
| run; | |
| %mend; | |
| %check_var_exist(dsn=class1); | |
| %check_var_exist(dsn=class2); | |
| %check_var_exist(dsn=class3); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment