BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Myurathan
Quartz | Level 8

Hi all!

 

I would have to use a dataset that is provided by another unit. last part of the dataset name(day part) changes each month, and rest stays the same. such as 

 

datasetxyz_20180523

datasetxyz_20180630

datasetxyz_20180710

 

I want my code to pick up the correct dataset when I run. If I am running on 2018 may, it should grab the dataset --> datasetxyz_20180523. 

 

Please help me how to achieve this. 

 

Thanks in advance

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Query dictionary.tables:

%let libname=mylib;
%let dsname=datasetxyz_;
%let period=201805;

proc sql;
select memname into :memname from dictionary.tables
where substr(memname,1,length("&dsname&period")) = upcase("&dsname&period") and libname = upcase("&libname");
quit;

data want;
set &libname..&memname;
run;

 

View solution in original post

5 REPLIES 5
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Why should it pick up that one for 2018?  Is it always the first.  As a recommendation, which will be ignored, putting data - in this case a date - in dataset names will only lead to far harder programming to handle it.  One dataset with date as a column is so much simpler to work with from any angle, its then just a matter of 

where date_var="23May2018"d;

As it is, now your going to have to write code which delves in the metadata, converts the various information, then pulls the right file out.  Factoring up your work, the code you write, and the likelihood that it will fail 10 fold.

Maybe something like:

data inter;
  set sashelp.vtable where=(libname="<YOURLIB>" and substr(memname,1,10)="DATASETXYZ"));
  d_var=input(scan(memname,2,"_"),yymmdd8.);
  where year(d_var)=2018;
run;
proc sort data=inter;
  by d_var;
run;
data inter;
set inter;
if _n_=1;
run;

So this gets a list of datasets from a library which you need to specify (all uppercase), with that dataset prefix, gets the date part from the filename, and converts to a date, keeping only year 2018.  Then it sorts the data with earliest first and finally takes the first record.  All that code just because of a design choice, madness.

gamotte
Rhodochrosite | Level 12

Hello,

 

data datasetxyz_20180602;
set sashelp.class;
run;

proc sql noprint;
    SELECT MEMNAME
    INTO :dsname
    FROM dictionary.tables
    WHERE LIBNAME="WORK" AND MEMNAME LIKE cats("DATASETXYZ_",put(today(),yymmn6.),"%");
quit;

%put &dsname.;
Kurt_Bremser
Super User

Query dictionary.tables:

%let libname=mylib;
%let dsname=datasetxyz_;
%let period=201805;

proc sql;
select memname into :memname from dictionary.tables
where substr(memname,1,length("&dsname&period")) = upcase("&dsname&period") and libname = upcase("&libname");
quit;

data want;
set &libname..&memname;
run;

 

Myurathan
Quartz | Level 8
@Kurt_Bremser You are truly amazing. Thank you so much
s_lassen
Meteorite | Level 14

If you only get one dataset per month, there is a fast and dirty solution. Just wildcard the day part with a colon:

 

%let month=201806;

/* or perhaps: %let month=%sysfunc(date(),yymmn6.); */

 

Data want

  set <libname>.datasetxyz_&month: ;

 

Of course, if you get more than one dataset each month, this will read all of the datasets for the month. Which may not be what you want.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 1047 views
  • 3 likes
  • 5 in conversation