99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁精彩閱讀SAS SQL select…into語句創(chuàng)建宏變量巧妙解決問題的總結(jié)
SAS SQL select…into語句創(chuàng)建宏變量巧妙解決問題的總結(jié)
2017-03-29
收藏

SAS SQL select…into語句創(chuàng)建宏變量巧妙解決問題的總結(jié)

今在某SAS交流群看到這樣一個問題如下:

有一個數(shù)據(jù)集a,有三個變量c,b,d(他們在數(shù)據(jù)集中的順序也是如此),想新建一個變量var,并添加到b和d中間,怎么做?

我想的代碼如下:

/*將原數(shù)據(jù)集變量名稱存放在宏變量中*/

proc sql ;

select name into :names separated by ","

from dictionary.columns

where libname="SASHELP" and memname="CLASS";

quit;

%put &names;

/*解析宏變量,插入新變量名稱,在放入宏變量*/

data test;

vars="&names";

vars_new=tranwrd(vars,"Sex","Sex,'newvar' astest");

call symput("new_names",vars_new);

run;

%put &new_names;

/*在原數(shù)據(jù)集中插入新變量*/

proc sql;

create table test as

select &new_names

from sashelp.class;

quit;

這個問題思路很簡單,把原數(shù)據(jù)集變量組裝如宏變量,再按要求修改,再組裝到一個宏變量。在實(shí)際工作中可能基本不會遇到,如果遇到一般也是采用數(shù)據(jù)集間的橫向合并去解決這個問題。

在這里想說的是,怎么巧妙運(yùn)用SQL里select…into語句。如果你一直關(guān)注本公眾號的文章,你會發(fā)現(xiàn)在最近的文章里都涉及到該語句的使用,一個文件夾下文件的批量導(dǎo)入、一個EXCEL工作薄中不規(guī)則sheet名稱的批量導(dǎo)入,邏輯庫多個數(shù)據(jù)集的批量導(dǎo)出、變量的批量處理,數(shù)據(jù)集的批量合并等等,都巧妙的應(yīng)用了select…into..語句。

要巧妙運(yùn)用該語句,有時還需要理解下SAS的dictionary table(字典數(shù)據(jù)表)的應(yīng)用。首先需要字典數(shù)據(jù)表是什么,通過view字典數(shù)據(jù)表如下可知,通俗的說就是存放數(shù)據(jù)集基本信息的數(shù)據(jù)表,這個表包括存放列(columns)、存放數(shù)據(jù)集名稱(memname)和存放數(shù)據(jù)集所在邏輯庫名稱(libname)的數(shù)據(jù)表。

1.查詢數(shù)據(jù)集變量名稱 ,如果要把某數(shù)據(jù)集的變量名稱抽提出來就如上述例子;從dictionary.columns里查詢name;

2.查詢數(shù)據(jù)集名稱 如果要對某邏輯庫的不同數(shù)據(jù)集進(jìn)行批量,就把該邏輯庫的所有數(shù)據(jù)集名稱放入宏變量。就從dictionary.tables.

proc sql;

select memname INTO :memnames

from dictionary.tables

where libname="WORK";

quit;

3.查詢邏輯庫名稱  如果想對不同邏輯庫進(jìn)行操作,將邏輯庫名稱放入宏變量。

proc sql;

select distinct(libname) 

INTO:memnames separated by ","

from dictionary.tables;

quit;

注意的問題:

(1)以上查詢也都可以在dictionary.indexes里查詢,變量名稱、數(shù)據(jù)集和邏輯庫對應(yīng)名稱為name、memname和libname。

(2)變量名稱、數(shù)據(jù)集和邏輯庫名稱存放形式均為嚴(yán)格的大寫。如where libname="WORK";因此在查詢時需注意大小寫,如果小寫需要upcase下。

(3)需要根據(jù)宏變量運(yùn)用的場景選擇不同的分隔符,如sas base模塊中多個變量間不需要逗號隔開,這時宏變量變量名稱存放時分隔符就該是空格,相反在SQL中多個變量并放時,需要逗號作為分隔符,這時需要逗號分隔名稱等等,總之首先需要考慮宏變量的運(yùn)用場景添加合適的分隔符分隔名稱。

4.數(shù)據(jù)集變量數(shù)和觀測數(shù)

proc sql;

select nobs,nvar into:nobs,:nvars

from dictionary.tables

where libname="WORK" and memname="TEST";

quit;

5.跨邏輯庫數(shù)據(jù)集名稱 從多個邏輯庫里查詢數(shù)據(jù)集,并批量導(dǎo)出。

/*不同邏輯庫不同數(shù)據(jù)集*/

proc sql;

select  distinct trim(libname||"."|| memname)

into :libnames separated by " "

from dictionary.indexes;

quit;

%put &libnames;

綜上,想怎么操作,就看你的解決問題的思路了。很簡單,問題簡單化,就是這么簡單和任性。

理解了以上問題,在處理批量問題時其實(shí)是很簡單的。請結(jié)合以前發(fā)的文章進(jìn)行理解,當(dāng)然還有很多別的應(yīng)用,只要是能想到的,你能把它轉(zhuǎn)化為sas能識別的語言,一切很簡單。

1. 導(dǎo)入含有多個不規(guī)則EXCEL子表名稱的數(shù)據(jù) ——SAS+EXCLE合作更有效

2. SAS data步、SQL和宏解決多表(≥3)合并問題

3. 一段宏解決TOPSIS綜合評價問題,寫文章也就是批量生產(chǎn)的問題

4. Sas應(yīng)用之解決多個文件數(shù)據(jù)資源的批量導(dǎo)入問題

5. Sas 數(shù)組、宏應(yīng)用之批量更改變量類型。


SQL

數(shù)據(jù)分析咨詢請掃描二維碼

若不方便掃碼,搜微信號:CDAshujufenxi

數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機(jī)數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進(jìn)行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個參數(shù)驗(yàn)證碼對象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺檢測極驗(yàn)服務(wù)器是否宕機(jī) new_captcha: data.new_captcha, // 用于宕機(jī)時表示是新驗(yàn)證碼的宕機(jī) product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說明請參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計(jì)時完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }