Help using Base SAS procedures

Wildcarding Variable names

Reply
N/A
Posts: 1

Wildcarding Variable names

Hi,

I'd like to know if there is a way of formatting all the date variables using a wildcard naming convention. For instance, if you have 4
date variables (datestart, dateend, datedue and datestopped), you can use the following; 'format date: ddmmyy8.;' but if the

variables were called startdate, enddate, duedate and stopdate, is there a way to wildcard the variable name from the beginning ?

Thanks for any help offered.

Super User
Posts: 19,822

Re: Wildcarding Variable names

Posted in reply to crayfordsean

Not really Smiley Sad

You could create a list of all variables that end in date using dictionary.Columns/sashelp.vcolumn but it may not be worth it.

Super Contributor
Posts: 308

Re: Wildcarding Variable names

Posted in reply to crayfordsean

Hello,

As Reeza stated you may use the dictionary. Something like this:

proc sql noprint;
select name into :vars separated by ' ' from sashelp.vcolumn
where memname='CLASS' and libname='SASHELP' AND name like '%e';
quit;

%put &vars;

data want;
informat &vars $15.; 
set sashelp.class (drop=&vars) ;
run;

Super User
Posts: 19,822

Re: Wildcarding Variable names

Posted in reply to crayfordsean

IIf the variables are side by side you can use --

format firstvar--lastvar date9.;

PROC Star
Posts: 7,480

Re: Wildcarding Variable names

Posted in reply to crayfordsean

If the variables aren't side by side here is a working example of what Fareeza suggested:

data have;

  input startdate x enddate y duedate stopdate;

  cards;

1 2 3 4 5 6

2 3 4 5 6 7

;

proc sql noprint;

  select name into :vars

    separated by ' '

      from sashelp.vcolumn

      where libname='WORK' and

            memname='HAVE' and

            name like '%date'

  ;

quit;

data want;

  set have;

  format &vars. date9.;

run;

Respected Advisor
Posts: 3,799

Re: Wildcarding Variable names

If you put the FORMAT statement after the SET it won't reorder the variables in HAVE. 

PROC Star
Posts: 7,480

Re: Wildcarding Variable names

Posted in reply to data_null__

Good point! I changed it in my post.

Ask a Question
Discussion stats
  • 6 replies
  • 241 views
  • 0 likes
  • 5 in conversation