How to put values entered in a prompt into a table

100 views Asked by At

I created a prompt called &CPF where I enter several values, I would like to put all the values entered in a table. Prompt

I've tried a few things but nothing worked.

data TESTE; set original (keep=&TESTE); run;

Would anyone know how to do this?

Thanks in advice

1

There are 1 answers

3
Stu Sztukowski On

When you have a multi-valued prompt, each value is stored in its own macro variable. In your case, we'll suppose that your prompt is named TESTE. You want to keep only the variables that the user enters.

Suppose we're going to run this on sashelp.cars. We want to keep the variables Make, Model, and Horsepower. Create a test program with a prompt named TESTE that runs the following code:

%put _USER_;

This displays all user-defined macro variables. Let's run the program with the prompt and check the log.

enter image description here

You will see the following macro variables:

GLOBAL TESTE Make
GLOBAL TESTE0 3
GLOBAL TESTE1 Make
GLOBAL TESTE2 Model
GLOBAL TESTE3 Horsepower
GLOBAL TESTE_COUNT 3

TESTE1-TEST3 holds each invidiual value, and TESTE_COUNT gives you the total count of macro varibles. You can use this to loop through and get each value. Note that if you only have one input, only &TESTE will be created rather than numbered values. We can create a loop that starts at 2 to account for this.

%macro teste;
    data teste;
        set sashelp.cars(keep=&teste 
                              %do i = 2 %to &teste_count.;
                                  &&teste&i
                              %end;
                        );
    run;
%mend;
%teste;

You can resolve all of these macro variables into a space-separated list by creating a macro which returns it when you specify the prompt name.

%macro get_prompt_values(name);
    %let list=&&&name.;

    %do i = 2 %to &&&name._count;
        %let list = &list &&&name.&i;
    %end;
    
    &list
%mend;

data teste;
    set sashelp.cars(keep=%get_prompt_values(teste));
run;