BookmarkSubscribeRSS Feed
arde
Obsidian | Level 7

Hi,

 

I have a folder with a 30-40 datasets.  I have created a macro to look for dates within those datasets but keep getting errors for datasets that don't have dates in them.   

 

How can I create a macro for fields that only have "Date" in the variable name and will check all datasets within a folder?

 

 

For example,

 

data path.dates;
FirstName $ LastName $ StartDate EndDate ;
datalines;
Bill Smith 201902 20190301
Ben Junior 2019 20190131
;
run;

 

data path.nondates;
FirstName $ LastName$  City $;
datalines;
Linda Davis NY
Sue Smith LA
;
run;

 

 

want to add dashes to all fields within the folder that have "Date" in the variable name:

 

proc sql;

create table path.? as
select *, case when length(strip(Date?)) = 8 then catx("-", substr(strip(Date?), 1,4) ,substr(strip(Date?),5,2),
substr(strip(Date?),7, 2))
when length(strip(Date?)) = 6 then catx("-", substr(Date?, 1,4),substr(Date?,5,2))
else Date?
end as Date? from path.?
;
Quit;

 

 

 

Thanks for your help

 

1 REPLY 1
Reeza
Super User

Assuming these are SAS datasets use the SASHELP.VCOLUMNS table instead.

 

libname myFiles 'path to my SAS files';

proc sql;
create table date_vars as
select *
from sashelp.vcolumn
where upcase(libname)='MYFILES' and upcase(name) like '%DATE%';
quit;

@arde wrote:

Hi,

 

I have a folder with a 30-40 datasets.  I have created a macro to look for dates within those datasets but keep getting errors for datasets that don't have dates in them.   

 

How can I create a macro for fields that only have "Date" in the variable name and will check all datasets within a folder?

 

 

For example,

 

data path.dates;
FirstName $ LastName $ StartDate EndDate ;
datalines;
Bill Smith 201902 20190301
Ben Junior 2019 20190131
;
run;

 

data path.nondates;
FirstName $ LastName$  City $;
datalines;
Linda Davis NY
Sue Smith LA
;
run;

 

 

want to add dashes to all fields within the folder that have "Date" in the variable name:

 

proc sql;

create table path.? as
select *, case when length(strip(Date?)) = 8 then catx("-", substr(strip(Date?), 1,4) ,substr(strip(Date?),5,2),
substr(strip(Date?),7, 2))
when length(strip(Date?)) = 6 then catx("-", substr(Date?, 1,4),substr(Date?,5,2))
else Date?
end as Date? from path.?
;
Quit;

 

 

 

Thanks for your help

 


 

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 1 reply
  • 568 views
  • 1 like
  • 2 in conversation