How to use the scan for a numeric variable [SAS]

2

I have a table like this:

List_ID 1 4 7 10

(In total there are 100 numbers)

I want to call them for a macro I've created. I was trying to do it with Scan, but I read that it was only for text variables. It gave me that my variables were not initialized.

Here is the code:

proc sql; 
select ID INTO: LISTA_ID SEPARATED BY '*' from 
WORK.AMOSTRA;
run;


PROC SQL;
SELECT COUNT(*) INTO: NR SEPARATED BY '*' FROM
WORK.AMOSTRA;
RUN;

%MACRO CICLO_teste();

%LET LIM_MSISDN = %EVAL(NR);
%LET I = %EVAL(1);

%DO %WHILE (&I<= &LIM_MSISDN);
%LET REF = %SCAN(LISTA_ID,&I,,'*'); 

DATA WORK.UP&REF;
SET WORK.BASE&REF;
FORMAT PERC_ACUM 9.3;
IF FIRST.ID_CLIENTE THEN PERC_ACUM=0;
PERC_ACUM+PERC;
RUN; 



%LET I = %EVAL(&I+1);
%END;
%MEND;

%CICLO_TESTE;

The idea is to run this macro for each of the ID's contained in LISTA_ID and referenced in work.base & ref and work.up & ref. How can I do it?

    
asked by anonymous 11.07.2015 / 02:00

1 answer

0

Hello! Firstly, I believe you are confusing the scan function (which receives only text values) with the % scan macro macro. Whenever you have the symbol "% " before the function name, it is a macro function, which takes a variable macro as an argument. In this way, the % scan macro function can also be used with numeric values.

I believe the warning that appeared was something like "WARNING: Macro variable LIM_MSISDN is not initialized" . This happened because you forgot to put the ampersand (symbol & ") before the text NR, indicating that NR is a variable macro, in this section here: % LET LIM_MSISDN =% EVAL (NR) . you do not need to use % eval here, you only use % eval when you are going to do some arithmetic operation with variable macro value, which is not the case here; here you just want to copy the value. I've put down two code suggestions that should work for what you want to do:

Code 1: I'm not sure if it works because I've never used '*' to separate elements into macro variables and I do not have SAS here to test:

proc sql; 
    select ID INTO: LISTA_ID SEPARATED BY '*' from 
    WORK.AMOSTRA;
quit;

%MACRO CICLO_teste;

    %let num_elementos = %sysfunc(countw(&LISTA_ID.), %str(*));

    %do i = 1 %to &num_elementos.;

        %let ref = %scan(&LISTA_ID., &i., *);

        DATA WORK.UP&REF;
            SET WORK.BASE&REF;
            FORMAT PERC_ACUM 9.3;
            IF FIRST.ID_CLIENTE THEN PERC_ACUM=0;
        PERC_ACUM+PERC;
    RUN; 
    %end;

%MEND CICLO_TESTE;
%CICLO_TESTE;

Code 2: Using space to separate variable macro elements:

proc sql; 
    select ID INTO: LISTA_ID SEPARATED BY ' ' from 
    WORK.AMOSTRA;
quit;

%MACRO CICLO_teste;

    %let num_elementos = %sysfunc(countw(&LISTA_ID.));

    %do i = 1 %to &num_elementos.;

        %let ref = %scan(&LISTA_ID., &i.,' ');/*Como usei espaço como separador, nem precisava colocar o terceiro argumento, mas deixei aqui pra ficar explícito*/

        DATA WORK.UP&REF;
            SET WORK.BASE&REF;
            FORMAT PERC_ACUM 9.3;
            IF FIRST.ID_CLIENTE THEN PERC_ACUM=0;
        PERC_ACUM+PERC;
    RUN; 
    %end;

%MEND CICLO_TESTE;
%CICLO_TESTE;
    
02.12.2018 / 04:21