Pular para conteúdo

Home

Buscar tabela mais recente por sufixo

Ao armazenar tabelas no formato TABELA_ANOMES (TABELA_202001, TABELA_202002 ...) tem-se a necessidade de buscar a tabela do mês mais recente.

1
2
3
4
5
6
proc sql noprint;
    select max(scan(memname,4,'_')) into :date
    from dictionary.tables
    where 
        libname = upcase("MEU_LOCAL") and memname like 'TABELA_%';
quit;

SAS Sintaxe - Datas e Posições

Buscar mês e ano atual

Ano e Mês atual por macro
%LET ANO = %sysfunc(year("&sysdate"d));
%LET MES = %sysfunc(month("&sysdate"d));
Criando ANOMES por macro
1
2
3
4
5
%LET MES_INI    = 1;
%LET MES_FIM    = %sysfunc(month("&sysdate"d));
%LET MES        = %sysfunc(month("&sysdate"d));
%LET ANOMES_INI = %eval(100*&ANO + &MES_INI);
%LET ANOMES     = %eval(100*&ANO + &MES_FIM);

Convertendo yyyymmdd de caractere para data

Convertendo de string pra date
1
2
3
4
5
data want_where_num;
  set have_with_num_date;
  date_new = input(put(date,8.), yymmdd8.);
  format date_new weekdate9. ;
run;
Criando datetime
PROC SQL;
    CREATE TABLE BIC2_COUNT AS
    SELECT 
        MEMNAME
        ,NOBS
        ,dhms('23nov21'd, 23, 6, 11) format datetime20.  as posicao
    from dictionary.tables
    where 
        libname = "BIC2" AND 
        MEMNAME IN (&MEMNAMES)      
;QUIT;