sas sql应用技巧

COALESCE function
In our administrative tables, the Social Security Number (SSN) field is not very well populated. We need to go after different sources: membership tables (old, current, and daily) and other utilization tables. PROC SQL makes the selection process very easy, where the COALESCE function will pick the first non-missing value.
PROC SQL; CREATE TABLE _SSNINFO AS
SELECT S.*, COALESCE (C.SSN, A.SSN, B.SSN, D.SSN) AS SSN,
FROM _SAMPLE AS S LEFT JOIN _CMS AS A ON S.HRN=A.HRN
LEFT JOIN _MG AS B ON S.HRN=B.HRN
LEFT JOIN _CMSDL AS C ON S.HRN=C.HRN
LEFT JOIN _DOCPLUS AS D ON S.HRN=D.HRN
;QUIT;

Access other databases
Below is SQL code to query an Oracle table from PC SAS. (OO.CMS_MEMBER is our membership file with 400,000+ rows). PROC SQL is the only way you can join a SAS table and an Oracle table. Method 1 uses join while Method 2 uses subquery. As we can see from the chart on the next page Method 2 is super fast for a small sample.
D 1--;
*--METHOPROC SQL;
CREATE TABLE TTT AS
SELECT B.HRN, FAMACT, RELTN
FROM XSAMPLE AS A INNER JOIN OO.CMS_MEMBER (dbkey=hrn dbindex=yes) AS B
ON A.HRN =B.HRN
;QUIT;
*--METHOD 2--;
PROC SQL NOPRINT; SELECT HRN INTO :HRN_LIST SEPARATED BY ',' FROM xsample; QUIT;
PROC SQL;
CREATE TABLE TTT AS
SELECT HRN, FAMACT, RELTN
FROM OO.CMS_MEMBER (dbkey=hrn dbindex=yes)
WHERE HRN IN (&HRN_LIST)
;QUIT;
Note: The maximum for &HRN_LIST under Windows SAS version 8, for 8 digits HRN, is about 1000 HRNs; in other words, the SQL limit comes before SAS

Textwrapping
When you have a long character variable (such as a COMMENT field in the questionnaire), and you want to print the values using PROC PRINT, you will get the warning message:
WARNING: Data too long for column "COMMENT"; truncated to 124 characters to fit.
A simple solution is to use PROC SQL with the flow option. An alternative would be to use PROC REPORT. (Note: Flow=30 has an effect on all character variables.)
PROC SQL FLOW=30;
SELECT HRN, COMMENT
FROM A
;QUIT;
HRN COMMENT
--------------------------------------
12345678 LONGTEXTTTTTTTTTTTTTTTTTTTTT
TTTTTTTTTTTTTTTTTTTTTTTTTTTT
TTTTTTTTTTTTTTTTTTTTTTTTTTTT
TTTTTTTTTTTTTTTTTTTTTTTTTTTT
87654321 LONGTEXTTTTTTTTTTTTTTTTTTTTT
TTTTTTTTTTTTTTTTTTTTTTTTTTTT
TTTTTTTTTTTTTTTTTTTTTTTTTTTT
TTTTTTTTTTTTTTTTTTTTTTTTTTTT

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值