Hi, I have 5 folders with different dates which have some sas datasets.
trying to read the folder so that later i can pick the sas dataset of m interest.
below code throws an errors.
ERROR: Argument 1 to function PUTN referenced by the %SYSFUNC or %QSYSFUNC macro function is not a number.
ERROR: Invalid arguments detected in %SYSCALL, %SYSFUNC, or %QSYSFUNC argument list. Execution of %SYSCALL statement or %SYSFUNC
or %QSYSFUNC function reference is terminated.
%let dt=%sysfunc(putn("2019-05-10"d, yymmdd10.));
%put &dt;
any ideas how to read folders with yyyy-mm-dd format to pick the sas datasets ?
Wrong date format, try:
%let dt=%sysfunc(putn("10may2019"d, yymmdd10.));
%put &dt;
SAS date literal is in the format "DDMmmYYYY"d
All the best
Bart
did you even try running the code from @yabwon or myself above, it seems to give you exactly what you are asking for.
To use a date literal, "xxxxxx"d, the string inside the quotes needs to be something that the DATE informat can understand. Your mistake is trying to use year month day style strings as date literals. If you already have the date in yymmdd10 style then you don't need to use the PUTN() function call at all.
%let dt=2019-05-10;
libname want "c:\my_folder\my_subfolder\&dt" ;
Well, that's so simple its frightening. Why didn't I think of that?
%let dt=%sysfunc(putn("10may2019"d, yymmddd10.));
%put &=dt;
produces the result 2019-05-10
that's what you want, right?
Hi,
try with this example:
/* generate test data in folders */
options dlcreatedir;
libname x "C:\my_folder\";
libname x "C:\my_folder\my_subfolder\";
libname x "C:\my_folder\my_subfolder\2019-05-10";
data x.class1; set sashelp.class; run;
libname x "C:\my_folder\my_subfolder\2019-06-10";
data x.class2; set sashelp.class; run;
libname x "C:\my_folder\my_subfolder\2019-07-10";
data x.class3; set sashelp.class; run;
libname x "C:\my_folder\my_subfolder\2019-08-10";
data x.class4; set sashelp.class; run;
libname x "C:\my_folder\my_subfolder\2019-09-10";
data x.class5; set sashelp.class; run;
libname x "C:\my_folder\my_subfolder\2019-10-10";
data x.class6; set sashelp.class; run;
libname x "C:\my_folder\my_subfolder\2019-11-10";
data x.class7; set sashelp.class; run;
libname x clear;
/* macro-loop to assign libraries */
%macro generateLibrary(start, number);
%do i = 1 %to &number.;
%let dt=%sysfunc(intnx(month, &start.,%eval(&i. - 1) ,S), yymmdd10.);
libname lib&i "C:\my_folder\my_subfolder\&dt.";
%end;
%mend generateLibrary;
%generateLibrary('10may2019'd, 7);
and adopt it to your needs
All the best
Bart
It is hard to give an answer when you haven't explained the actual problem.
If you know the folder names then just use them.
Are you saying you don't know the folder names and you need to try to figure them out?
Can't you just ask the operating system to tell you? If you are using Windows then use DIR command, if Unix then ls command. Read the output into a dataset and you know the folder names.
If you cannot do that (perhaps the NOXCMD option is set?) and you want to find all of the folders for a range of dates then just loop over that range of dates and check if the folder exists.
%do date=%sysevalf("01MAY2018"d) %to %sysevalf("01JAN2010"d);
%let dt=%sysfunc(putn(&date,yymmdd10.));
%let path=c:\myfolders\&dt ;
%if %sysfunc(fileexist("&path")) %then %do;
.... what ever you need to do when you find one ...
%end;
%end;
1) You can create a list of subFolders like below:
/* generate test data in folders */
options dlcreatedir;
libname x "C:\my_folder\";
libname x "C:\my_folder\my_subfolder\";
data x.class01 x.class02 x.class03; set sashelp.class; run;
libname x "C:\my_folder\my_subfolder\2019-05-10";
data x.class1; set sashelp.class; run;
libname x "C:\my_folder\my_subfolder\2019-06-10";
data x.class2; set sashelp.class; run;
libname x "C:\my_folder\my_subfolder\2019-07-10";
data x.class3; set sashelp.class; run;
libname x "C:\my_folder\my_subfolder\2019-08-10";
data x.class4; set sashelp.class; run;
libname x "C:\my_folder\my_subfolder\2019-09-10";
data x.class5; set sashelp.class; run;
libname x "C:\my_folder\my_subfolder\2019-10-10";
data x.class6; set sashelp.class; run;
libname x "C:\my_folder\my_subfolder\2019-11-10";
data x.class7; set sashelp.class; run;
libname x clear;
/* extract the list of subdirectories */
data exactFolderNames;
length exactFolderName $ 512;
keep exactFolderName;
base = "C:\my_folder\my_subfolder";
rc=filename("_TMP_", base);
did=dopen("_TMP_");
do i = 1 to dnum(did);
subfolder = dread(did, i);
rc = filename("_sTMP_", catx("/", base, subfolder));
subdid = dopen("_sTMP_");
if subdid = 0 then continue; /* to ignore files */
/* put (_all_) (=/) / ;*/
exactFolderName = dinfo(subdid, "Directory");
output;
rc = dclose(subdid);
rc = filename("_sTMP_");
end;
did=dclose(did);
rc=filename("_TMP_");
run;
All the best
Bart
@sahoositaram555 wrote:
Hi, I have 5 folders with different dates which have some sas datasets.
trying to read the folder so that later i can pick the sas dataset of m interest.
below code throws an errors.
ERROR: Argument 1 to function PUTN referenced by the %SYSFUNC or %QSYSFUNC macro function is not a number.
ERROR: Invalid arguments detected in %SYSCALL, %SYSFUNC, or %QSYSFUNC argument list. Execution of %SYSCALL statement or %SYSFUNC
or %QSYSFUNC function reference is terminated.
%let dt=%sysfunc(putn("2019-05-10"d, yymmdd10.));
%put &dt;
any ideas how to read folders with yyyy-mm-dd format to pick the sas datasets ?
You do not need to read the directory to get the names of SAS datasets. Assign a library to the directory and query DICTIONARY.TABLES (or SAHELP.VTABLE).
To see what your problem is, and how it can be avoided, see this:
%let dt=%sysfunc(putn("10may2019"d, yymmdd10.));
%put &dt;
%let dt=%sysfunc(putn(%sysfunc(inputn(2019-05-10,yymmdd10.)),yymmddd10.));
%put &dt;
%let dt=2019-05-10;
%put &dt.;
All three assignments create the same result.
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.
Ready to level-up your skills? Choose your own adventure.