BookmarkSubscribeRSS Feed
Hagay
SAS Employee

בשבועות האחרונים ראינו כמה דוגמאות לשימוש בפונקציות של SAS לשם קבלת מידע ותפעול של קבצים במערכת ההפעלה. השבוע אנחנו נסכם את הנושא בדוגמא קצרה לבעיה נפוצה – יצירת טבלת SAS עם רשימת כל הקבצים (תיקיות משנה) בתוך תיקיה כלשהי.

 

ישנן, כמובן, דרכים רבות לבצע את המשימה הזאת אבל אנחנו נראה אפשרות אחת שעושה שימוש במגוון פונקציות מובנות של SAS שנוצרו בדיוק לשם כך.

 

אחרי הקוד יש קישור לתיעוד של SAS עבור כל אחת מהפונקציות בהן עשינו שימוש כאשר כל אחת מהן מכילה בתחתית הדף של התיעוד קישורים לפונקציות דומות נוספות כך שאתם מוזמנים להמשיך לפשפש בתיעוד של SAS כדי לגלות יכולות ודוגמאות נוספות.

%let my_folder=C:\Temp\test;

data FOLDER_MEMBERS(drop=i rc did memcount);
	length 
		Member_Path	$256
		Is_File_CD	$1
		File_Extension	$3;
	rc = filename("mydir", "&my_folder"); * Defining a filename to the folder;
	did = dopen("mydir"); * Opening the folder;
	memcount = dnum(did); * Number of members in a folder;
	do i=1 to memcount;
		member_path=dread(did, i); * Getting the name of each member (file or subfolder) in our folder;
		rc = mopen(did, member_path); * Checking if the member is a file or a folder;
		if rc=0 then do; * It is a sub folder);
			is_file_cd='N';
			file_extension='N/A';
		end;
		else do;
			is_file_cd='Y'; * It is a file;
			file_extension = ifc(findc(member_path, '.')>0, lowcase(scan(member_path, -1, '.')), ''); * Showing off our SAS prowess;
		end;
		member_path=catx('\',"&my_folder", member_path); * Adding the path to the member name to get full member path;
		output;
	end;
	rc = dclose(did);	* Closing the folder;
run;

הפונקציות הרלוונטיות בהן הקוד שלנו עושה שימוש הן:

DOPEN

DNUM

DREAD

MOPEN

DCLOSE

 

חגי

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

Discussion stats
  • 0 replies
  • 345 views
  • 0 likes
  • 1 in conversation