BookmarkSubscribeRSS Feed
sahoositaram555
Pyrite | Level 9

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 ?

14 REPLIES 14
yabwon
Onyx | Level 15

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

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



sahoositaram555
Pyrite | Level 9
Hi @yabwon,
Folders i have are in YYYY-MM-DD eg: 2019-05-10. Is there any other way out to read the folder and pick the dataset of my interest or shall i convert the folder name to 10may2019 by applying date9.? Kindly suggest

yabwon
Onyx | Level 15
Could you give some more detailed example? I'm not sure if I correctly understood your request.

Do you have folder like: "C:\my_folder\my_subfolder2019-05-10" and want to read data set from that folder?

Bart
_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



sahoositaram555
Pyrite | Level 9
Hi @yabwon, Thanks for your concern. i have 6 folders "C:\my_folder\my_subfolder\2019-05-10" to "C:\my_folder\my_subfolder\2019-11-10" . Every folder contains some ****.sas7bdat datasets. I'm in the process of creating a macro to pick 2 .sas7bdat dataset of my interest from each folder which is required for merging in later stage.

so, i was trying to use %let dt=%sysfunc(putn("2019-05-10"d, yymmdd10.)) for start date to use it in do loop. but some how error comes up because the date is not being read by sas. Hope I'm clear.

PaigeMiller
Diamond | Level 26

@sahoositaram555 

did you even try running the code from @yabwon or myself above, it seems to give you exactly what you are asking for.

--
Paige Miller
Tom
Super User Tom
Super User

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" ;

 

PaigeMiller
Diamond | Level 26

Well, that's so simple its frightening. Why didn't I think of that?

--
Paige Miller
sahoositaram555
Pyrite | Level 9
Thank you both @PaigeMiller and @Tom for your thoughts .
I actually have them in YYYY-MM-DD format. The errors i was getting as i was using them in %let statement without quotes.
Thanks for making me clarify on this.
PaigeMiller
Diamond | Level 26

 

%let dt=%sysfunc(putn("10may2019"d, yymmddd10.));
%put &=dt;

produces the result 2019-05-10

that's what you want, right?

 

 

 

--
Paige Miller
yabwon
Onyx | Level 15

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

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



sahoositaram555
Pyrite | Level 9
Hi @yabwon, Thank you for sharing a sample code. I should have mentioned it before that the difference isn't one. Though i know that in my last post i had mentioned 2019-05-10 to 2019-11-10, but it's not exactly with of equal difference(-1) between folders. The difference varies.
i understand the statement %let dt=%sysfunc(intnx(month, &start.,%eval(&i. - 1) ,S), yymmdd10.); will only work when difference of folders are only a month.
So, any other way of dealing this if the difference between folders not consistent,unlike 1 month as assumed?
And it throws error when the library names generated from the statement libname lib&i "C:\my_folder\my_subfolder\&dt."; is taken for any further proc process.
eg: instead of C:\my_folder\my_subfolder\ the sample i have is
"\\XXX-sas-zz1\my_folder\sub_folder. So when i use a procedure
proc sort data=lib.&i;by PatientInitials;run; it throws error as i have \\XXX-sas-zz1 and due to the "-" delimiters sas is unable to recognize the path and throws error.
Tom
Super User Tom
Super User

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;

 

yabwon
Onyx | Level 15

Hi @sahoositaram555 

 

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

 

 

 

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



Kurt_Bremser
Super User


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

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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
  • 14 replies
  • 2930 views
  • 1 like
  • 5 in conversation