BookmarkSubscribeRSS Feed
Hagay
SAS Employee

בדר"כ אין חשיבות אמיתית לסדר של העמודות בטבלה. כל עוד העמודה קיימת והיא מהסוג והגדול הנכונים התהליכים שלנו יעבדו בצורה תקינה.

 

לעיתים אבל אנחנו מעדיפים שהעמודות כן יופיעו בסדר מסוים, למשל כדי שיהיה לנו יותר נוח לבדוק את הנתונים במבט או פשוט כי אנחנו שואפים לשלמות והעובדה שהעמודות לא מופיעות בסדר הנכון מונעת מאיתנו שינה בלילה.

 

ניתן לפתור את הבעיה בעזרת קצת קוד:

* Creating a sample table;
proc sql noprint;
	create table CARS as 
	select 
		make, 
		cylinders,
		count(*) as Records
	from 
		SASHELP.CARS
	where 
		cylinders^=.
	group by 
		1,2;
quit;

proc transpose data=CARS out=CARS_T(drop=_name_) prefix=CYL;
	by make;
	var records;
	id cylinders;
run;

* The actual example starts here; * Getting the columns we want in a specific order; proc sql noprint; create table CARS_COLUMNS as select name from dictionary.columns where libname='WORK' and memname='CARS_T' and name like 'CYL%'; run; * To get the columns with a numeric suffix to sort properly we had to dig deep into the documentation; proc sort data=CARS_COLUMNS sortseq=linguistic(numeric_collation=on); by name; run; * Creating the table with the columns properly ordered; * I really like my generated code to be formatted just like a user written code; filename sascode temp; data _null_; set CARS_COLUMNS end=last; file sascode; if _n_=1 then do; put 'proc sql noprint;'; put 'create table CARS_T1 as'; put 'select'; put 'make,'; end; put name +(-1)@; * I want the comma in the same row as the column name and with no space between them - not really needed; if ^last then put ','; else do; put / 'from CARS_T;'; * The / symbol is to have the statement in a new line; put 'quit;'; end; run; %include sascode;* Executing the code we have just generated;

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

חגי

1 REPLY 1
EyalGonen
Lapis Lazuli | Level 10

יש מקרים בהם סדר העמודות עשוי לעזור מבחינת ביצועים. המאמר https://support.sas.com/resources/papers/proceedings/proceedings/forum2007/108-2007.pdf מציין את זה

 

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
  • 1 reply
  • 434 views
  • 2 likes
  • 2 in conversation