i found a code that list all directories and sub directories from a path.
but it brings only the directory and the name of the file.
could you guys please help me how to bring the owner and the file size?
%macro list_files(dir);
%local filrf rc did memcnt name i;
%let rc=%sysfunc(filename(filrf,&dir));
%let did=%sysfunc(dopen(&filrf));
%if &did eq 0 %then %do;
%put Directory &dir cannot be open or does not exist;
%return;
%end;
%do i = 1 %to %sysfunc(dnum(&did));
%let name=%qsysfunc(dread(&did,&i));
%if %index(%qscan(&name,-1,'/'),.) gt 0 %then %do;
data _tmp;
length dir $512 name $100;
dir=symget("dir");
name=symget("name");
run;
proc append base=want data=_tmp;
run;quit;
%end;
%else %if %qscan(&name,2,.) = %then %do;
%list_files(&dir/&name)
%end;
%end;
%let rc=%sysfunc(dclose(&did));
%let rc=%sysfunc(filename(filrf));
%mend list_files;
DOPTNAME is your friend here.
Read SAS documentation for "Example 1: Using DOPTNAME to Retrieve Directory Attribute Information"
This example opens the directory with the fileref MYDIR, retrieves all system-dependent directory information items, writes them to the SAS log, and closes the directory:
%let rc=%sysfunc(filename(filrf, physical-name));
%let did=%sysfunc(dopen(&filrf));
%let infocnt=%sysfunc(doptnum(&did));
%do j=1 %to &infocnt;
%let opt=%sysfunc(doptname(&did, &j));
%put Directory information=&opt;
%end;
%let rc=%sysfunc(dclose(&did));
%macro test;
%let filrf=mydir;
%let rc=%sysfunc(filename(filrf, physical-name));
%let did=%sysfunc(dopen(&filrf));
%let infocnt=%sysfunc(doptnum(&did));
%do j=1 %to &infocnt;
%let opt=%sysfunc(doptname(&did, &j));
%put Directory information=&opt;
%end;
%let rc=%sysfunc(dclose(&did));
%mend test;
%test
Use the finfo() function. You can do this all in a single data step.
Documentation
/* Macro variable to store the directory. Do not keep ending slash. */
%let directory = /my/directory;
filename mydir "&directory";
data file_list;
length directory
filetype $15.
filename $1000.
owner $100.
size 8.
;
directory = "&directory/";
/* Open the directory */
did = dopen("mydir");
/* If the directory exists, loop through all files in the directory */
if(did) then do;
do i = 1 to dnum(did);
/* Get the filename */
filename = dread(did, i);
/* Create a filename variable and create a file ID to read its attributes */
rc = filename('infile', cats(directory,filename));
fid = fopen('infile');
owner = finfo(fid, 'Owner Name');
size = finfo(fid, 'File Size (bytes)');
/* Flag if it's a directory or file */
if(missing(size)) then filetype = 'Directory';
else filetype = 'File';
/* Close the file */
rc = fclose(fid);
output;
end;
end;
/* Close the directory */
rc = close(did);
keep directory filename owner size filetype;
run;
Related
My code exports a table from SAS to a excel file in a folder.
My goal to change the export folder to a test folder if I set the variable test to 1.
This is my code:
/*Delete .bak files*/
%macro xlsx_bak_delete(file, file_name) / des='Delete backup spreadsheets';
option mprint notes;
data _null_;
fname = 'todelete';
rc = filename(fname, "&file.&file_name..xlsx.bak");
rc = fdelete(fname);
rc = filename(fname);
run;
%mend xlsx_bak_delete;
/*Export*/
%macro ExportExcel(path,file_name,table_name);
proc export data=&table_name
outfile="&path.&file_name..xlsx"
dbms=xlsx
replace;
;
run;
%xlsx_bak_delete(&export_path, &file_name)
%mend;
%LET test = 1
%IF test = 1 %then %do;
/*TEST export path */
%Let export_path = \\Bfd1\b00369\Afdeling\HS-OKO\Oko\Likviditet\Likviditetsstyring\LCR\Daglig LCR - Axiom\Test\;
%end;
%else %do;
/*Export path*/
%Let export_path = \\Bfd1\b00369\Afdeling\HS-OKO\Oko\Likviditet\Likviditetsstyring\LCR\Daglig LCR - Axiom\Test_prod\;
%end;
/*Datetiemstamp macro*/
%let fileTimeStamp = %sysfunc(date(), ddmmyyd10.)_%sysfunc(putc(%sysfunc(time(), b8601TM6.), $6.)) ;
%put &fileTimeStamp.;
%ExportExcel(&export_path,DAGLIGEKORREKTIONER_&fileTimeStamp.,QUERY_FOR_DAGLIGEKORREKTIONER);
The issue is that my code only exports to the test_prod folder nothing changes if test is = 0 or 1 why is that?
You are incorrectly referencing the macro variable. As a result, it will always end up in the %else %do; part of the %if statement.
You can use the variable by referencing it with an ampersand preceding its name (&test in your case), which is called a macro variable reference.
You are also missing a semicolon ; after the %LET statement.
%LET test = 1;
%IF &test = 1 %then %do;
/*TEST export path */
%Let export_path = \\Bfd1\b00369\Afdeling\HS-OKO\Oko\Likviditet\Likviditetsstyring\LCR\Daglig LCR - Axiom\Test\;
%end;
%else %do;
/*Export path*/
%Let export_path = \\Bfd1\b00369\Afdeling\HS-OKO\Oko\Likviditet\Likviditetsstyring\LCR\Daglig LCR - Axiom\Test_prod\;
%end;
Hi I want to get all attachments from SAS content server of solution EGRC 6.1 policy object and want to save it in a physical location on my server.
This is what I'm doing now.
proc sql noprint;
select BUSINESS_OBJECT_RK
into: rk saperated by '-'
from sasoprsk.attachment_l
where BUSINESS_OBJECT_NM eq "POLICY_INST"
and ATTACHMENT_TYPE_CD ne "LNK";
select FILE_NM
into: file saperated by '-'
from sasoprsk.attachment_l
where BUSINESS_OBJECT_NM eq "POLICY_INST"
and ATTACHMENT_TYPE_CD ne "LNK";
quit;
%macro attachment;
proc sql noprint;
select count(*)
into: count
from sasoprsk.attachment_l
where BUSINESS_OBJECT_NM eq "POLICY_INST"
and ATTACHMENT_TYPE_CD ne "LNK";
quit;
%do i = 1 %to &count;
filename out temp;
%let rk_l=%scan(%bquote(&rk), %bquote(&i) ,%str(-));
%let file_l=%scan(%bquote(&file), %bquote(&i) ,%str(-));
%put "file &file_l";
%put "http://sasbap.demo.sas.com/SASContentServer/repository/default/sasdav/Products/SASEnterpriseGRC/EnterpriseGRCMidTier6.1/Content/policy/&rk_l/&file_l";
proc http
method="get"
url="http://sasbap.demo.sas.com/SASContentServer/repository/default/sasdav/Products/SASEnterpriseGRC/EnterpriseGRCMidTier6.1/Content/policy/&rk_l/&file_l"
webUserName="sas"
webPassword="Orion123"
out=out;
run;
%end;
%mend;
%attachment;
I'm saving my attachment files in temp file but I want to save in a physical location as "C drive" inside a folder named as their rk of my objrct with proper extension as file.doc, file.xls or file.jpg etc.
example
obj_nm rk file_nm
POLICY_INST 12 file.xls
POLICY_INST 13 file.doc
POLICY_INST 14 file.gif
I want to put those files as
C:/12/file.xls
C:/13/file.doc
C:/14/file.gif
Kindly tell me how can I save my files from SAS content server to a physical location of my server with proper extension.
Simply add the following before your proc http call:
%mf_mkdir(C:/&rk_l)
filename out "C:/&rk_l/&file_l";
The source code for mf_mkdir is available in the open source MacroCore library, and is reproduced below:
/**
#file
#brief Creates a directory, including any intermediate directories
#details Works on windows and unix environments via dcreate function.
Usage:
%mf_mkdir(/some/path/name)
#param dir relative or absolute pathname. Unquoted.
#version 9.2
#copyright GNU GENERAL PUBLIC LICENSE v3
**/
%macro mf_mkdir(dir
)/*/STORE SOURCE*/;
%local lastchar child parent;
%let lastchar = %substr(&dir, %length(&dir));
%if (%bquote(&lastchar) eq %str(:)) %then %do;
/* Cannot create drive mappings */
%return;
%end;
%if (%bquote(&lastchar)=%str(/)) or (%bquote(&lastchar)=%str(\)) %then %do;
/* last char is a slash */
%if (%length(&dir) eq 1) %then %do;
/* one single slash - root location is assumed to exist */
%return;
%end;
%else %do;
/* strip last slash */
%let dir = %substr(&dir, 1, %length(&dir)-1);
%end;
%end;
%if (%sysfunc(fileexist(%bquote(&dir))) = 0) %then %do;
/* directory does not exist so prepare to create */
/* first get the childmost directory */
%let child = %scan(&dir, -1, %str(/\:));
/*
If child name = path name then there are no parents to create. Else
they must be recursively scanned.
*/
%if (%length(&dir) gt %length(&child)) %then %do;
%let parent = %substr(&dir, 1, %length(&dir)-%length(&child));
%mf_mkdir(&parent);
%end;
/*
Now create the directory. Complain loudly of any errors.
*/
%let dname = %sysfunc(dcreate(&child, &parent));
%if (%bquote(&dname) eq ) %then %do;
%put ERROR: could not create &parent\&child;
%abort cancel;
%end;
%else %do;
%put Directory created: &dir;
%end;
%end;
/* exit quietly if directory did exist.*/
%mend;
This is a follow up question to this question.
I'm trying to simplify the way we embed images into our HTML results. The idea for this was inspired by this other question .
Basically what I am trying to do is to write a function-style macro (called %html_embed_image()) that takes an image, and converts it into a base64 format suitable for use in an HTML <img src=""> block.
Given an image such as this:
The usage would be:
data _null_;
file _webout;
put "<img src=""%html_embed_image(iFileName=hi.png)"" />";
run;
And the final output would be:
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAIAAAAC64paAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAABaSURBVDhP5YxbCsAgDAS9/6XTvJTWNUSIX3ZAYXcdGxW4QW6Khw42Axne81LG0shlRvVVLyeTI2aZ2fcPyXwPdBI8B999NK/gKTaGyxaMX8gTJRkpyREFmegBTt8lFJjOey0AAAAASUVORK5CYII=" />
The question linked above shows how to do this in regular datastep code, but I am having issues getting this working in a function style macro. I posted a simplified problem I was having earlier and Tom was able to solve that simplified issue, but it doesn't seem to be working in the greater context of the function style macro.
Here is my code so far (the line causing issues is wrapped with two put statements indicating that it is the problem):
option mprint symbolgen;
%macro html_embed_image(iFileName=);
%local rc fid rc2 str str_length format_length format_mod base64_format base64_string;
/* ONLY READ IN 16K CHUNKS AS CONVERTING TO BASE64 */
/* INCREASES SIZE AND DONT WANT TO EXCEED 32K. */
%let rc = %sysfunc(filename(filrf, &iFileName, , lrecl=16000));
%let fid = %sysfunc(fopen(&filrf, i, 16000, b));
%if &fid > 0 %then %do;
%let rc = %sysfunc(fread(&fid));
%do %while(&rc eq 0);
%let rc2 = %sysfunc(fget(&fid,str,16000));
%let str = %superq(str);
/* FORMAT LENGTH NEEDS TO BE 4n/3 ROUNDED UP TO NEAREST MULTIPLE OF 4 */
%let str_length = %length(&str);
%let format_length = %sysevalf(4*(&str_length/3));
%let format_mod = %sysfunc(mod(&format_length,4));
%if &format_mod ne 0 %then %do;
%let format_length = %sysevalf(&format_length - &format_mod + 4);
%end;
%let base64_format = %sysfunc(cats($base64x,&format_length,.));
%put &=base64_format;
/* CONVERT THE BINARY DATA TO BASE64 USING THE CALCULATED FORMAT */
%put PROBLEM START;
%let base64_string = %sysfunc(putc(&str,&base64_format));
%put PROBLEM END;
%put &=base64_string;
/*&base64_string*/ /* RETURN RESULT HERE - COMMENTED OUT UNTIL WORKING */
%let rc = %sysfunc(fread(&fid));
%end;
%end;
%else %do;
%put %sysfunc(sysmsg());
%end;
%let rc=%sysfunc(fclose(&fid));
%let rc=%sysfunc(filename(filrf));
%mend;
Test the code:
%put %html_embed_image(iFileName=hi.png);
Results in:
ERROR: Expected close parenthesis after macro function invocation not found.
Any tips on how to fix this, or suggestions for workarounds would be great.
Just write the text using a data step.
%let fname=hi.png;
data _null_;
file _webout recfm=n;
if _n_=1 then put '<img src="data:image/png;base64,';
length str $60 coded $80 ;
infile "&fname" recfm=n eof=eof;
do len=1 to 60;
input ch $char1.;
substr(str,len,1)=ch;
end;
put str $base64x80.;
return;
eof:
len=len-1;
clen=4*ceil(len/3);
coded = putc(substr(str,1,len),cats('$base64x',clen,'.'));
put coded $varying80. clen ;
put '" />';
run;
If you really want to generate text in-line it might be best to add quotes so that you could call the macro in the middle of a PUT statement and not worry about hitting maximum string length.
%macro base64(file);
%local filerc fileref rc fid text len ;
%*----------------------------------------------------------------------
Assign fileref and open the file.
-----------------------------------------------------------------------;
%let fileref = _fread;
%let filerc = %sysfunc(filename(fileref,&file));
%let fid = %sysfunc(fopen(&fileref,s,60,b));
%*----------------------------------------------------------------------
Read file and dump as quoted BASE64 text.
-----------------------------------------------------------------------;
%if (&fid > 0) %then %do;
%do %while(%sysfunc(fread(&fid)) = 0);
%do %while(not %sysfunc(fget(&fid,text,60)));
%let len = %eval(4*%sysfunc(ceil(%length(%superq(text))/3)));
%if (&len) %then "%sysfunc(putc(%superq(text),$base64x&len..))" ;
%end;
%end;
%let rc = %sysfunc(fclose(&fid));
%end;
%*----------------------------------------------------------------------
Clear fileref assigned by macro,
-----------------------------------------------------------------------;
%if ^(&filerc) %then %let rc = %sysfunc(filename(fileref));
%mend base64;
So then your example data step becomes something like this:
%let fname=hi.png;
data _null_;
file _webout recfm=n;
put '<img src="data:image/png;base64,' %base64(&fname) '" />' ;
run;
I'm trying to import series of CSV files with a macro that loops thru all the files in the given folder.But, there are some empty CSV files in the folder which I would like to exclude from the loop.
Is there any way in SAS to find CSV file size ?
PROC IMPORT OUT=&output
DATAFILE= "&input"
DBMS=CSV REPLACE;
GETNAMES=YES;
DATAROW=2;
*GUESSINGROWS=32000;
RUN;
Thanks, Sam.
Here's away to do it in a datastep:
filename fileref 'c:\date.tmp';
data a;
infile fileref truncover;
fid=fopen('fileref');
Bytes=finfo(fid,'File Size (bytes)');
crdate=finfo(fid,'Create Time');
moddate=finfo(fid,'Last Modified');
input var1 $20.;
run;
%macro FileAttribs(filename);
%local rc fid fidc Bytes;
%let rc=%sysfunc(filename(onefile,&filename));
%let fid=%sysfunc(fopen(&onefile));
%let Bytes=%sysfunc(finfo(&fid,File Size (bytes)));
%if &Bytes >0 %then %do;
%put #####Size is > 0#####;
%end;
%else %do;
%put #####Size is < 0#####;
%end;
%let fidc=%sysfunc(fclose(&fid));
%let rc=%sysfunc(filename(onefile));
%put NOTE: File size of &filename is &Bytes bytes;
%mend FileAttribs;
and here's another way to do it:
%let filename =c:\date.tmp;
%let rc=%sysfunc(filename(onefile,&filename));
%let fid=%sysfunc(fopen(&onefile));
%let Bytes=%sysfunc(finfo(&fid,File Size (bytes)));
%let fidc=%sysfunc(fclose(&fid));
%put NOTE: File size of &filename is &Bytes bytes;
Can not use Xcommands in SAS EG. No access to SAS Management Console. How can I get a list of files in a directory without using Xcommands?
Tried DINFO but can only get 1 piece of info. Need a list of all files in the selected directory. Am I missing something here?
data a;
rc=filename("mydir", c:\");
put "rc = 0 if the directory exists: " rc=;
did=dopen("mydir");
put did=;
numopts=doptnum(did);
put numopts=;
do i = 1 to numopts;
optname = doptname(did,i);
put i= optname=;
optval=dinfo(did,optname);
put optval=;
output;
end;
run;
I've not used Enterprise Guide but how about using a pipe'd filename? You cn then use that with the infile statement to put the result of the query into a dataset...
filename dirlist pipe "ls /<your-path>/*";
data dirlist ;
infile dirlist ;
format fname $300. ;
input fname $ ;
run;
Here's a couple of macros we use to do this. The main macro is %file_list but it also requires the %isDir macro in order to run. Some usage examples:
%put %file_list(iPath=e:\blah\); * TEST AGAINST A DIR THAT DOESNT EXIST;
%put %file_list(iPath=e:\SASDev); * TEST AGAINST A DIR THAT EXISTS;
%put %file_list(iPath=e:\SASDev\,iFiles_only=1); * LIST ONLY FILES;
%put %file_list(iPath=e:\sasdev\,iFiles_only=1,iFilter=auto); * FILTER TO ONLY FILES THAT CONTAIN THE STRING AUTO;
%isDir macro definition:
/******************************************************************************
** PROGRAM: CMN_MAC.ISDIR.SAS
**
** DESCRIPTION: DETERMINES IF THE SPECIFIED PATH EXISTS OR NOT.
** RETURNS: 0 IF THE PATH DOES NOT EXIST OR COULD NOT BE OPENED.
** 1 IF THE PATH EXISTS AND CAN BE OPENED.
**
** PARAMETERS: iPath: THE FULL PATH TO EXAMINE. NOTE THAT / AND \ ARE TREATED
** THE SAME SO &SASDIR/COMMON/MACROS IS THE SAME AS
** &SASDIR\COMMON\MACROS.
**
******************************************************************************/
%macro isDir(iPath=,iQuiet=1);
%local result dname did rc;
%let result = 0;
%let check_file_assign = %sysfunc(filename(dname,&iPath));
%put ASSIGNED FILEREF (0=yes, 1=no)? &check_file_assign &iPath;
%if not &check_file_assign %then %do;
%let did = %sysfunc(dopen(&dname));
%if &did %then %do;
%let result = 1;
%end;
%else %if not &iQuiet %then %do;
%put &err: (ISDIR MACRO).;
%put %sysfunc(sysmsg());
%end;
%let rc = %sysfunc(dclose(&did));
%end;
%else %if not &iQuiet %then %do;
%put &err: (ISDIR MACRO).;
%put %sysfunc(sysmsg());
%end;
&result
%mend;
%filelist macro definition:
/******************************************************************************
** PROGRAM: MACRO.FILE_LIST.SAS
**
** DESCRIPTION: RETURNS THE LIST OF FILES IN A DIRECTORY SEPERATED BY THE
** SPECIFIED DELIMITER. RETURNS AN EMPTY STRING IF THE THE
** DIRECTORY CAN'T BE READ OR DOES NOT EXIST.
**
** PARAMETERS: iPath: THE FULL PATH TO EXAMINE. NOTE THAT / AND \ ARE TREATED
** THE SAME SO &SASDIR/COMMON/MACROS IS THE SAME AS
** &SASDIR\COMMON\MACROS. WORKS WITH BOTH UNIX AND WINDOWS.
**
******************************************************************************/
/*
** TODO. THERES ABOUT 100 WAYS THIS COULD BE IMPROVED SUCH AS SIMPLIFYING IF STATEMENTS FOR FILTERS...
*/
%macro file_list(iPath=, iFilter=, iFiles_only=0, iDelimiter=|);
%local result did dname cnt num_members filename rc check_dir_exist check_file_assign;
%let result=;
%let check_dir_exist = %isDir(iPath=&iPath);
%let check_file_assign = %sysfunc(filename(dname,&iPath));
%put The desired path: &iPath;
%if &check_dir_exist and not &check_file_assign %then %do;
%let did = %sysfunc(dopen(&dname));
%let num_members = %sysfunc(dnum(&did));
%do cnt=1 %to &num_members;
%let filename = %qsysfunc(dread(&did,&cnt));
%if "&filename" ne "" %then %do;
%if "&iFilter" ne "" %then %do;
%if %index(%lowcase(&filename),%lowcase(&iFilter)) eq 0 %then %do;
%goto next;
%end;
%end;
%if &iFiles_only %then %do;
%if %isDir(iPath=%nrbquote(&iPath/&filename)) %then %do;
%goto next;
%end;
%end;
%let result = &result%str(&iDelimiter)&filename;
%next:
%end;
%else %do;
%put ERROR: (CMN_MAC.FILE_LIST) FILE CANNOT BE READ.;
%put %sysfunc(sysmsg());
%end;
%end;
%let rc = %sysfunc(dclose(&did));
%end;
%else %do;
%put ERROR: (CMN_MAC.FILE_LIST) PATH DOES NOT EXIST OR CANNOT BE OPENED.;
%put %sysfunc(sysmsg());
%put DIRECTORY EXISTS (1-yes, 0-no)? &check_dir_exist;
%put ASSIGN FILEREF SUCCESSFUL (0-yes, 1-no)? &check_file_assign;
%end;
/*
** RETURN THE RESULT. TRIM THE LEADING DELIMITER OFF THE FRONT OF THE RESULTS.
*/
%if "&result" ne "" %then %do;
%qsubstr(%nrbquote(&result),2)
%end;
%mend;
%let path=C:\ETC;
filename parent "&path\Data\CSV";
data files;
length file_name $50;
drop rc did i;
did=dopen("parent");
if did > 0 then do;
do i=1 to dnum(did);
file_name=dread(did,i);
output;
end;
rc=dclose(did);
end;
else put 'Could not open directory';
run;
* Some additions;
%global name;
%global count2;
%let name=;
%let count2=;
proc sql;
select file_name into :name separated by '*' from work.files;
%let count2 = &sqlobs;
quit;
This works fine. I use &name for other macro and do something with files... (load from CSV, for example).