簡體   English   中英

宏到表或帶有sas的宏變量

[英]macro into a table or a macro variable with sas

我有這個宏。 目的是從表dicofr中獲取變量名,然后使用symput將其中的行放入變量名中。

但是,某些操作無法正常進行,因為該變量&nvarname未被視為變量。

這是dico&&pays&l

varname descr
var12   aza
var55   ghj
var74   mcy

這是dico&&pays&l..1

varname 
var12
var55
var74

下面是我的代碼

%macro testmac;

%let pays1=FR ;

%do l=1 %to 1 ;

data dico&&pays&l..1 ; set dico&&pays&l (keep=varname); 
call symput("nvarname",trim(left(_n_))) ;
run ;


data a&&pays&l;
set a&&pays&l;
nouv_date=mdy(substr(date,6,2),01,substr(date,1,4));
format nouv_date monyy5.;
run;



proc sql;
create table toto 
(nouv_date date , nomvar varchar (12));
quit;

proc sql;

insert into toto SELECT max(nouv_date),"&nvarname" as nouv_date as varname FROM a&&pays&l WHERE (&nvarname ne .);


%end;

%mend;

%testmac;

附屬問題。 是否可以將變量名和與該變量名相關的日期放入宏變量中? 我的男人告訴了我有關此事,但我從未做過。

提前致謝。

編輯:我有這張桌子

date    col1 col2 col3 ... colx
1999M12 .    .    .        .
1999M11 .    2    .        .
1999M10 1    3    .        3
1999M9  0.2  3    2        1

我想知道日期最大的列的名稱,知道列內的值與缺少的值不同。

對於col1,它將是1999M10。 對於col2,它將是1999M11等...

在我看來,您正在嘗試使用宏來生成INSERT INTO語句來填充表。 我可能完全不使用宏就可以做到這一點。

您可以使用datastep語句將INSERT INTO語句寫出到文件中。 然后按照數據步驟,使用%include語句運行文件。

這將更易於編寫/維護/調試,並且性能也更好。

根據您的更新,我認為以下代碼可以滿足您的需求。 如果您不介意先對輸入數據集進行排序,則可以通過單個數據步驟獲得所需的所有值-無需宏!

data have;
length date $7;
input date col1 col2 col3;
format date2 monyy5.;
date2 = mdy(substr(date,6,2),1,substr(date,1,4));
datalines;
1999M12 .    .    .   
1999M11 .    2    .   
1999M10 1    3    .   
1999M09 0.2  3    2   
;
run;

/*Required for the following data step to work*/
/*Doing it this way allows us to potentially skip reading most of the input data set*/
proc sort data = have;
  by descending date2;
run;

data want(keep = max_date:);
  array max_dates{*} max_date1-max_date3;
  array cols{*} col1-col3;
  format max_date: monyy5.;

  do until(eof); /*Begin DOW loop*/
    set have end = eof;

    /*Check to see if we've found the max date for each col yet.*/
    /*Save the date for that col if applicable*/
    j = 0;
    do i = 1 to dim(cols);
      if missing(max_dates[i]) and not(missing(cols[i])) then max_dates[i] = date2;
      j + missing(max_dates[i]);
    end;
    /*Use j to count how many cols we still need dates for.*/
    /* If we've got a full set, we can skip reading the rest of the data set*/
    if j = 0 then do;
      output;
      stop;
    end;
  end; /*End DOW loop*/
run;

編輯:如果要輸出每個名稱的最大日期旁邊的名稱,可以稍加修改即可完成:

data want(keep = col_name max_date);
  array max_dates{*} max_date1-max_date3;
  array cols{*} col1-col3;
  format max_date monyy5.;

  do until(eof); /*Begin DOW loop*/
    set have end = eof;

    /*Check to see if we've found the max date for each col yet.*/
    /*If not then save date from current row for that col*/
    j = 0;
    do i = 1 to dim(cols);
      if missing(max_dates[i]) and not(missing(cols[i])) then max_dates[i] = date2;
      j + missing(max_dates[i]);
    end;
    /*Use j to count how many cols we still need dates for.*/
    /* If we've got a full set, we can skip reading the rest of the data set*/
    if j = 0 or eof then do;
      do i = 1 to dim(cols);
        col_name = vname(cols[i]);
        max_date = max_dates[i];
        output;  
      end;
      stop;
    end;
  end; /*End DOW loop*/
run;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM