DATA Step, Macro, Functions and more

Can we apply filters on labels in a dataset ?

Reply
Occasional Contributor
Posts: 6

Can we apply filters on labels in a dataset ?

[ Edited ]

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 !?? 

Super User
Posts: 17,963

Re: Can we apply filters on labels in a dataset ?

No.

 

If you explain your situation and problem with some detail perhaps we can make alternative suggestions. 

Trusted Advisor
Posts: 1,115

Re: Can we apply filters on labels in a dataset ?

Hi @pradeepvaranasi,

 

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;

 

 

Super User
Posts: 17,963

Re: Can we apply filters on labels in a dataset ?

IMO that doesn't use the labels in filter, except in a roundabout way. Theoretically can also use vlabel but depends on what OP wants.
Super User
Posts: 10,550

Re: Can we apply filters on labels in a dataset ?

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).

Trusted Advisor
Posts: 1,115

Re: Can we apply filters on labels in a dataset ?

Hi @pradeepvaranasi,

 

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.

Ask a Question
Discussion stats
  • 5 replies
  • 294 views
  • 0 likes
  • 4 in conversation