SAS中文论坛

 找回密码
 立即注册

扫一扫,访问微社区

查看: 712|回复: 0
打印 上一主题 下一主题

So Many Variables; Too Many Labels, Moving Labels from One V

[复制链接]

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
楼主
 楼主| 发表于 2011-3-2 05:14:51 | 只看该作者

So Many Variables; Too Many Labels, Moving Labels from One V

From LCChien's blog on blogspot

原文載點:<a href="http://support.sas.com/resources/papers/proceedings10/047-2010.pdf"><!-- m --><a class="postlink" href="http://support.sas.com/resources/papers/proceedings10/047-2010.pdf">http://support.sas.com/resources/papers ... 7-2010.pdf</a><!-- m --></a><br /><br />在 SAS 資料處理的過程中,使用者會發現,當複製一個變數並給予新的變數名稱時,舊變數的格式雖然會一併複製到新變數裡面,但舊變數的 label 卻沒有辦法複製過去。若只有一兩個變數時還可以用手動的方式在 data step 裡面重打,但若變數過多時,這就會成為一個相當棘手的問題。John Ladds 提供了一個簡易的方式來解決這個 label 複製的問題。<br /><br /><a name='more'></a>以下面這個資料集當作範例:<br /><code><pre>proc format;<br />&nbsp;&nbsp; value OriginalOrderAgree<br />&nbsp;&nbsp; &nbsp; &nbsp; 1 = 'Strongly agree'<br />2 = 'Somewhat agree'<br />3 = 'Neither agree nor disagree'<br />4 = 'Somewhat disagree'<br />5 = 'Strongly disagree'<br />6 = 'Not applicable'<br />&nbsp;&nbsp; &nbsp; &nbsp;97 = "Don't know"<br />99 = 'Not stated';<br />&nbsp;&nbsp; value ReverseOrderAgree<br />&nbsp;&nbsp; &nbsp; &nbsp; 5 = 'Strongly agree'<br />4 = 'Somewhat agree'<br />3 = 'Neither agree nor disagree'<br />2 = 'Somewhat disagree'<br />1 = 'Strongly disagree'<br />&nbsp;&nbsp; &nbsp; &nbsp; other='Missing';<br />run;<br />** Create some test data;<br />data work.theData;<br />&nbsp;&nbsp; input @01 id &nbsp; &nbsp;$10.<br />&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; @11 Q001 &nbsp; &nbsp;2.<br />&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; @13 Q002 &nbsp; &nbsp;2.<br />&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; @15 Q003 &nbsp; &nbsp;2.<br />&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; @17 Q004 &nbsp; &nbsp;2.<br />&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; @19 Q005 &nbsp; &nbsp;2.<br />&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; @21 Q006 &nbsp; &nbsp;2.<br />&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; @23 Q007 &nbsp; &nbsp;2.<br />&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; @25 Q008 &nbsp; &nbsp;2.<br />&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; @27 Q009 &nbsp; &nbsp;2.<br />&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; @29 Q010 &nbsp; &nbsp;2.;<br />&nbsp;&nbsp; label Q001 = "Have material's and equipment I need";<br />&nbsp;&nbsp; label Q002 = "Material and tools avail in lang choice";<br />&nbsp;&nbsp; label Q003 = "I use the language of my choice";<br />&nbsp;&nbsp; label Q004 = "My jobs fits my interests.";<br />&nbsp;&nbsp; label Q005 = "I have support at work.";<br />&nbsp;&nbsp; label Q006 = "I am satisfied, current work arrangement";<br />&nbsp;&nbsp; label Q007 = "I can claim overtime";<br />&nbsp;&nbsp; label Q008 = "Overall, I like my job.";<br />&nbsp;&nbsp; label Q009 = "I get satisfaction from my work.";<br /><br />&nbsp;&nbsp;&nbsp;label Q010 = "I know how my work contributes";<br />format Q001-Q010 OriginalOrderAgree.;<br />datalines4;<br />1234567890 1 2 3 4 5 6 1 2 3 4;<br />1234567892 2 3 4 5 1 1 2 3 4 5;<br />1234567893 3 4 5 1 2 2 2 3 5 1;<br />1234567894 4 5 1 2 3 3 3 3 1 2;<br />1234567895 5 1 2 3 4 4 4 4 2 3;<br />1234567896 1 2 3 4 5 5 5 5 3 4;<br />1234567897 2 3 4 5 1 1 1 1 4 5;<br />1234567898 3 4 5 1 2 2 2 2 5 1;<br />1234567899 4 598 2 3 3 3 3 1 2;<br />123456781099 199 3 4 4 4 5 2 3;<br />;;;;<br />run;</pre></code><br />根據這個資料集,裡面有十個變數,從Q001~Q010,每個變數為數據「1,2,3,4,5」。現在假設要製造新變數R_Q001~R_Q010,而這些新變數是將就舊變數的數據倒轉過來,也就是 1 變成 5,2 變成 4,...,5 變成 1。於是作者利用下面這個 macro 來處理這段:<br /><code><pre>%macro reverseValueOrder(Q=,Qfmt=);<br />&nbsp;&nbsp; select(&amp;Q.);<br />&nbsp;&nbsp; &nbsp; when(1) &nbsp; R_&amp;Q.=5;<br />&nbsp;&nbsp; &nbsp; when(2) &nbsp; R_&amp;Q.=4;<br />&nbsp;&nbsp; &nbsp; when(3) &nbsp; R_&amp;Q.=3;<br />&nbsp;&nbsp; &nbsp; when(4) &nbsp; R_&amp;Q.=2;<br />&nbsp;&nbsp; &nbsp; when(5) &nbsp; R_&amp;Q.=1;<br />&nbsp;&nbsp; &nbsp; otherwise R_&amp;Q.=.;<br />&nbsp;&nbsp; end;<br />&nbsp;&nbsp; ** Transfer the questions label to the new variable;<br />&nbsp;&nbsp; <b><span class="Apple-style-span" style="color: red;">if eof then call execute("label R_&amp;Q.="||'"'||strip(vlabel(&amp;Q.))||'";');</span></b><br />&nbsp;&nbsp; ** Add the new value labels to the new variable;<br />&nbsp;&nbsp; format R_&amp;Q. &amp;Qfmt..;<br />%mend reverseValueOrder;</pre></code><br />其中,「select(&amp;Q);.....end;」主要就是在做變數倒轉的工作,而下面有一行「if...then...」搭配「call execute」便是在處理 label 複製的動作。裡面的 vlabel 函數的作用是將舊變數裡面的 label 給列出來,放在 strip 函式後面主要是移除不必要的空格,並確保 vlabel 函式叫出來的 label 是字串形式。<br /><br />接著就可以在新的 data step 裡面一邊進行數據倒轉的工作,一邊進行搬移 label 的工作,如下所示:<br /><code><pre>data work.thedata;<br />&nbsp;&nbsp; set work.theData <b><span class="Apple-style-span" style="color: red;">end=eof</span></b>;<br />** Open the post DATA STEP Processing;<br /><b><span class="Apple-style-span" style="color: red;">if eof then call execute('data &amp;syslast.; set &amp;syslast.;');&nbsp;</span></b><br />**A. My Job World &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;;<br />&nbsp;&nbsp; %reverseValueOrder(Q=Q001, Qfmt=ReverseOrderAgree);<br />&nbsp;&nbsp; %reverseValueOrder(Q=Q002, Qfmt=ReverseOrderAgree);<br />&nbsp;&nbsp; %reverseValueOrder(Q=Q003, Qfmt=ReverseOrderAgree);<br />&nbsp;&nbsp; %reverseValueOrder(Q=Q004, Qfmt=ReverseOrderAgree);<br />&nbsp;&nbsp; %reverseValueOrder(Q=Q005, Qfmt=ReverseOrderAgree);<br />&nbsp;&nbsp; %reverseValueOrder(Q=Q006, Qfmt=ReverseOrderAgree);<br />&nbsp;&nbsp; %reverseValueOrder(Q=Q007, Qfmt=ReverseOrderAgree);<br />&nbsp;&nbsp; %reverseValueOrder(Q=Q008, Qfmt=ReverseOrderAgree);<br />&nbsp;&nbsp; %reverseValueOrder(Q=Q009, Qfmt=ReverseOrderAgree);<br />&nbsp;&nbsp; %reverseValueOrder(Q=Q010, Qfmt=ReverseOrderAgree);<br />;... ... ... ... ... ... ... ... ... ... ... ** &nbsp;<br />** Close the post DATA STEP Processing;<br /><b><span class="Apple-style-span" style="color: red;">if eof then call execute ('run;');</span></b><br />run;</pre></code><br />特別提醒的地方有三個。一是 set 後面一定要加上「end=eof」字樣。二是執行 macro 前加上「if eof then call execute('data &amp;syslast.; set &amp;syslast.;');&nbsp;」,目的是確保程式執行的資料是當前資料。所以記得在運作此程式之前,別去跑其他不相干的資料。三是結束前加上「if eof then call execute ('run;');」,這樣就大功告成了。<div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6268919072942670865-7497945167349595128?l=sugiclub.blogspot.com' alt='' /></div>
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|手机版|Archiver|SAS中文论坛  

GMT+8, 2025-6-11 11:21 , Processed in 0.070493 second(s), 19 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表