Search code examples
automationsasdo-loops

SAS do-loop question for creating SAS data subcategories conditionally



data period;
set output.Sample_Y_n_deln;
if delnum >= 181 and delnum <= 184;
run;

data period2;
set output.Sample_Y_n_deln;
if delnum >= 185 and delnum <= 188;
run;

data period3;
set output.Sample_Y_n_deln;
if delnum >= 189 and delnum <= 192;
run;

Is there a way to automate this using some kind of loop? That point of this exercise is to get quarterly time slices for my data set based on delnum which is the date in a numeric format specific for this set.

I have heard of proc timeseries which seems applicable on first glance but I do not know much about it.


Solution

  • Another option is to use formats, but it depends on what you're doing next. If you want summaries by the period for example this works fine. There are also multiple ways to use the format in various other procs and data steps.

    proc format;
    value delnum_fmt
    181 - 184 = "Period 1"
    185 - 188 = "Period 2"
    189 - 192 = "Period 3"
    other = "Outside Period of Interest"
    ;
    run;
    
    proc freq data=output.sample_y_n_deln;
    table delnum;
    format delnum delnum_fmt.;
    run;
    

    Here is a good introductory reference on formats in SAS, and of course the documentation reference.