Skyler
Skyler

Reputation: 170

SAS %let statement to equal a called upon value from a table?

Is it possible to assign a %let statement to equal a called upon value from a table? I know I can do this in a loop with the following code.

proc sql noprint;
    select NameID into :Name separated by " "
    from Registration;
quit;

proc sql noprint;
    select count (*) into :NumNAME
    from Registration;
quit;

Is there a way to do it outside of a loop, or in only one step, by creating some %let Name = to something to just directly call upon a table? I realize this is similar to a previous question, I'm just more picky.

Upvotes: 0

Views: 2156

Answers (1)

Tim Sands
Tim Sands

Reputation: 1068

%Let statements aren't very flexible for this sort of thing.

CALL SYMPUT, however, can be used in the middle of datasteps to set macro variables and would probably help more.

How About:

data myReg;
INPUT Name $;
DATALINES;
Alex
Alex
Ben
Ben
Ben
Calvin
Calvin
Calvin
Calvin
;
run;

proc sort data=myReg; by name;run;
data MakeSomeMacroVars;
        set myReg end=LastRow;
        by name;

        length Allnames $30000;*Variable for space separated list of names;
        retain AllNames ' ';

        cnt+1;
        if last.name THEN DO;
                *Create a macro variable NUM_Alex or similar, with value equal to cnt;
                CALL SYMPUT (cats('NUM_',name),cats((put(cnt,12.))));
                cnt=0;
                *Add name to space separated list;
                Allnames = catx(" ",AllNames, name);
        END;

        if LastRow THen do;
                Call Symput ('AllNames',cats(compbl(AllNames)));
        END;
RUN;
%Put Alex has --&NUM_Alex--; * --> 2;
%Put Ben has --&NUM_Ben--; * --> 3;
%Put List of names is: --&AllNames--; * --> Alex Ben Calvin;

Upvotes: 2

Related Questions