Search code examples
sqlselectsasselect-into

Dynamize range of SAS PROC SQL SELECT INTO macro creation


I want to put multiple observations into an own macro variable. I would do this by using select into :obs1 - :obs4, however, as count of observations can differ, i would like to dynamize the range and my code looks like this:

proc sql;
    create table segments as select distinct substr(name,1,6) as segment from dictionary.columns
    where libname = 'WORK' and memname = 'ALL_CCFS' and name ne 'MONTH';
run;

proc sql noprint;
    select count(*) into: count from segments;
run;

proc sql noprint;
    select segment into :segment_1 - :segment_&count. from dictionary.columns;
run;

However, this doesn't seem to work... any suggestions? Thank you!


Solution

    • Leave last value empty/blank and SAS will create them automatically
    • Set it to an absurdly large number and SAS will only use what's required
    • Use a data step to create it where you can dynamically increment your number (not shown).

      proc sql noprint;
      select segment into :segment_1 - 
      from dictionary.columns;
      run;
      
      
      proc sql noprint;
      select segment into :segment_1 - :segment_999
      from dictionary.columns;
      run;