[prev in list] [next in list] [prev in thread] [next in thread] 

List:       sas-l
Subject:    Improved: Creating datasets based on variable values and a dynamic keep within one datastep?
From:       Roger DeAngelis <rogerjdeangelis () GMAIL ! COM>
Date:       2016-08-26 16:54:44
Message-ID: 3080160810556104.WA.rogerjdeangelisgmail.com () listserv ! uga ! edu
[Download RAW message or body]

Improved: Creating datasets based on variable values and a dynamic keep within one \
datastep?

https://goo.gl/qFJSsL
https://communities.sas.com/t5/General-SAS-Programming/Macros-for-creating-datasets-based-on-variable-values/m-p/292969


HAVE

Up to 40 obs from class total obs=19

Obs    NAME       SEX    AGE    HEIGHT    WEIGHT

  1    Alice       F      13     56.5       84.0
  2    Barbara     F      13     65.3       98.0
  3    Carol       F      14     62.8      102.5
  4    Jane        F      12     59.8       84.5
  5    Janet       F      15     62.5      112.5
  6    Joyce       F      11     51.3       50.5
  7    Judy        F      14     64.3       90.0
  8    Louise      F      12     56.3       77.0
  9    Mary        F      15     66.5      112.0
 10    Alfred      M      14     69.0      112.5
 11    Henry       M      14     63.5      102.5
 12    James       M      12     57.3       83.0
 13    Jeffrey     M      13     62.5       84.0
 14    John        M      12     59.0       99.5
 15    Philip      M      16     72.0      150.0
 16    Robert      M      12     64.8      128.0
 17    Ronald      M      15     67.0      133.0
 18    Thomas      M      11     57.5       85.0
 19    William     M      15     66.5      112.0

WANT


Up to 40 obs from M total obs=10

Obs    NAME       SEX    AGE    HEIGHT    WEIGHT

  1    Alfred      M      14     69.0      112.5
  2    Henry       M      14     63.5      102.5
  3    James       M      12     57.3       83.0
  4    Jeffrey     M      13     62.5       84.0
  5    John        M      12     59.0       99.5
  6    Philip      M      16     72.0      150.0
  7    Robert      M      12     64.8      128.0
  8    Ronald      M      15     67.0      133.0
  9    Thomas      M      11     57.5       85.0
 10    William     M      15     66.5      112.0


Up to 40 obs from F total obs=9

Obs    NAME       SEX    AGE    HEIGHT    WEIGHT

 1     Alice       F      13     56.5       84.0
 2     Barbara     F      13     65.3       98.0
 3     Carol       F      14     62.8      102.5
 4     Jane        F      12     59.8       84.5
 5     Janet       F      15     62.5      112.5
 6     Joyce       F      11     51.3       50.5
 7     Judy        F      14     64.3       90.0
 8     Louise      F      12     56.3       77.0
 9     Mary        F      15     66.5      112.0


%symdel SEX;
%put &=sex;

proc datasets;
 delete m f;
;run;quit;

NEW SOLUTION 1 (FASTEST you can over specify the number of levels)

* if you know just the upper limit on the number of levels;
data _null_;
  retain index 1;
  array BeenThere[32] $1 _temporary_ ;
  set sashelp.class;
  if whichc(sex,of BeenThere[*])=0 then do;
       BeenThere[index]=sex;
       if index=32 then do;
          putlog "More than 32 levels increase the dimension of BeenThere";
          stop;
       end;
       Index=Index+1;
       call symputx('sex',sex);
       rc=dosubl(resolve('
        data &sex;
          set sashelp.class(where=(sex="&sex"));
        run;quit;
        '));
  end;
;run;quit;

SOLUTION 2

* Probably not the most efficient method;

proc sort data=sashelp.class out=class;
by sex;
;run;quit;

data _null_;
  set class;
  by sex notsorted;
  if first.sex then do;
    call symputx('sex',sex);
     rc=dosubl(resolve('
      data &sex;
        set class(where=(sex="&sex"));
      run;quit;
    '));
  end;
;run;quit;


DYNAMIC KEEP;

This has other applications (meta data dynamic keep in one datastep address space)

HAVE1 (Meta data in excel - (configuation info for batch runs))

to 40 obs XEL.template total obs=1

  Obs      KEEP

    1    NAME SEX

HAVE2  (SASHELP.CLASS);

Obs    NAME       SEX    AGE    HEIGHT    WEIGHT

  1    Alfred      M      14     69.0      112.5
  2    Henry       M      14     63.5      102.5
  3    James       M      12     57.3       83.0
  4    Jeffrey     M      13     62.5       84.0
  5    John        M      12     59.0       99.5
  6    Philip      M      16     72.0      150.0
  7    Robert      M      12     64.8      128.0
  8    Ronald      M      15     67.0      133.0
  9    Thomas      M      11     57.5       85.0
 10    William     M      15     66.5      112.0

WANT  ( in a single datastep )

Obs    NAME       SEX

  1    Alfred      M
  2    Henry       M
  3    James       M
  4    Jeffrey     M
  5    John        M
  6    Philip      M
  7    Robert      M
  8    Ronald      M
  9    Thomas      M
 10    William     M

SOLUTION1

libname xel "d:/xls/template.xls";
data _null_;
  set xel.template;
  call symputx('keep',keep);
     rc=dosubl(resolve('
      data want;
        set sashelp.class(keep=&keep);
      run;quit;
    '));
;run;quit;
libname xel clear;

SOLUTION2

%symdel keep;
data _null_;
  set xel.template(in=one);
  call symputx('keep',keep);
  %utl_submit_wps64(
  libname wpshelp 'C:\Program Files\SASHome\SASFoundation\9.4\core\sashelp';
  libname wpswrk '%sysfunc(pathname(work))';
  data wpswrk.want;
    set wpshelp.class(keep=&keep);
  run;
  );
run;quit;


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic