03-27-2016 04:46 PM - edited 03-28-2016 04:32 AM
For instance I have my variables - Labels in the below fomat:
CELL_CALL_CNT_M1 - count of cell calls-April
CELL_CALL_CNT_M2 - count of cell calls-May
CELL_CALL_CNT_M3 - count of cell calls-June
CELL_CALL_CNT_M4 - count of cell calls-July
CELL_CALL_CNT_M5 - count of cell calls-August
CELL_CALL_CNT_M6 - count of cell calls-September
Say, I'd like to filter out data for the months of April, May & June. So I was thinking if we can apply filters on labels.
Any suggestions !??
03-27-2016 05:44 PM
If you mean something like "restrict a dataset or apply a procedure to variables whose labels meet certain criteria," then yes: We can create the corresponding variable list by means of PROC SQL and DICTIONARY.COLUMNS and then use it in a suitable statement or dataset option.
Example: Restrict PROC PRINT output of SASHELP.DEMOGRAPHICS to country name and variables whose labels mention the year 2005.
proc sql noprint; select name into :vlist separated by ' ' from dictionary.columns where libname='SASHELP' & memname='DEMOGRAPHICS' & findw(label, '2005'); quit; proc print data=sashelp.demographics(obs=10) label; var isoname &vlist; run;
03-27-2016 05:51 PM
03-28-2016 11:13 AM
Does this imply that at some time you have lables with values for April associated with a different variable name? If not then just keep the correct variables. If the differing names is the case then the data should be normalized (probably transposed ) to include a separate variable to indicate the month and then the exercise becomes selecting the values of month (or date even).
03-28-2016 11:40 AM
Thanks for adding more details to your initial post. It seems that the technique I suggested is applicable. Here's how:
/* Create test data */ data have; label CELL_CALL_CNT_M1 = 'count of cell calls-April' CELL_CALL_CNT_M2 = 'count of cell calls-May' CELL_CALL_CNT_M3 = 'count of cell calls-June' CELL_CALL_CNT_M4 = 'count of cell calls-July' CELL_CALL_CNT_M5 = 'count of cell calls-August' CELL_CALL_CNT_M6 = 'count of cell calls-September' ; length C: 8; run; /* dataset with 6 variables */ /* Select variables */ proc sql noprint; select name into :vlist separated by ' ' from dictionary.columns where libname='WORK' & memname='HAVE' & scan(label,2,'-') in ('April','May','June'); quit; data want; set have(keep=&vlist); run; /* dataset with 3 variables */
As you see, you can choose the appropriate character function, be it FINDW (as in my first example), SCAN (as above), INDEX, ..., and logical operators to specify your filter criterion on the labels.