Posted By

sastechies on 01/26/10


Tagged

macros 9 sas 92 Analyze


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

mprabhuram


SAS Macro to simply analyze a dataset.


 / Published in: SAS
 

URL: http://sastechies.blogspot.com/2010/01/simple-sas-macro-to-analyze-sas-dataset.html

  1. /*
  2. Data analysis is a process of inspecting, cleaning, transforming, and modelling data with the goal of highlighting useful information,
  3. suggesting conclusions, and supporting decision making.
  4.  
  5. The first step in this process is to know about your data...
  6. - Know what kind of data are you dealing with i.e. financial, pharmaceutical etc.
  7. - If you have access to the data..quickly run a freq / means to get data-level information.
  8. - Depending upon your business specific questions we might have to slice and dice / summarize the data.
  9.  
  10. Here's a simple SAS Macro that would help you to quickly find information as to
  11. - what are the variables available / last modification date etc.
  12. - Give a frequency of occurences / min / max etc info depending on the type of the SAS column info...
  13. - Produce a HTML report.
  14.  
  15.  
  16. Steps to code the macro...
  17.  
  18. 1. Get the type of the variables proc contents
  19. 2. Apply a Proc means with min max of numeric variables...Note: Proc Means does not generate a report for Character variables.
  20. 3. Apply a Proc Freq for Character Variables
  21. 4. Print an ODS HTML report for the results
  22.  
  23. */
  24.  
  25. /* Explanation of the parameters of the Macro...
  26. %analyzeSASdsn(dsn,numobs,varlist);
  27. dsn - name of the dataset to analyze
  28. numobs - number of observations to include in the dataset
  29. varlist - List of Variables that you need to analyze...use _ALL_ if you want to include all the variables.
  30. */
  31.  
  32.  
  33.  
  34. %macro analyzeSASdsn(dsn,numobs,varlist);
  35.  
  36. %let numvars=; /* Initialize the number of numeric Variables */
  37. %let charvars=; /* Initialize the number of character Variables */
  38.  
  39.  
  40. %if %upcase(&varlist) eq _ALL_ %then /* If the user request for all the variables then assign appropriately */
  41. %do;
  42. %let numvars=_numeric_;
  43. %let charvars=_character_;
  44. %end;
  45. %else /* then find if the vars requested are numeric type or character type */
  46. %do;
  47.  
  48. /* split the varlist into individual macro var names vars1 vars2 etc*/
  49. %let num=1;
  50. %let vars&num=%scan(&varlist,&num,' ');
  51.  
  52. %do %while(&&vars&num ne );
  53. %let num=%eval(&num + 1);
  54. %let vars&num=%upcase(%scan(&varlist,&num,' '));
  55. %end;
  56.  
  57. /* Get the List of variables in the &dsn dataset and put
  58. All char variables in charvarlist macro variables
  59. All Num variables in numvarlist macro variable
  60. */
  61.  
  62. %let dsid=%sysfunc(open(&dsn,i));
  63. %let numvarlist=;
  64. %let charvarlist=;
  65. %do i=1 %to %sysfunc(attrn(&dsid,nvars));
  66. %if (%sysfunc(vartype(&dsid,&i)) = N) %then %let numvarlist=&numvarlist %upcase(%sysfunc(varname(&dsid,&i)));
  67. %if (%sysfunc(vartype(&dsid,&i)) = C) %then %let charvarlist=&charvarlist %upcase(%sysfunc(varname(&dsid,&i)));
  68. %end;
  69. %let rc=%sysfunc(close(&dsid));
  70.  
  71. %put numvarlist=&numvarlist charvarlist=&charvarlist;
  72.  
  73.  
  74. /* Now check the variables required to report in the above list and assign them to the right macro variables...
  75. All char variables in charvarlist macro variables
  76. All Num variables in numvarlist macro variable
  77. */
  78.  
  79. %do i=1 %to %eval(&num - 1);
  80. %if %index(&numvarlist,&&vars&i) %then %let numvars=&&vars&i &numvars;
  81. %if %index(&charvarlist,&&vars&i) %then %let charvars=&&vars&i &charvars;
  82. %end;
  83.  
  84. %put numvars=&numvars charvars=&charvars;
  85.  
  86. %end;
  87.  
  88. ods listing close;
  89. ods html body="&htmlfilepath";
  90.  
  91. /* Now analyze the dataset with the Specified variables */
  92.  
  93. proc contents data=&dsn;run; /* Put a Contents procedure */
  94.  
  95. %if &numvars ne %then
  96. %do;
  97. /* Get Summary statistics of All the Numeric Variables with means procedure */
  98. proc means data=&dsn(obs=&numobs) n mean max min range;
  99. var &numvars;
  100. title 'Summary Statistics of all Numeric variables in the dataset';
  101. run;
  102. %end;
  103.  
  104. %if &charvars ne %then
  105. %do;
  106. /* Get Summary statistics of All the Character Variables with Freq procedure */
  107. proc freq data=&dsn(obs=&numobs);
  108. tables &charvars;
  109. title1 'Summary Statistics of all Character variables in the dataset';
  110. run;
  111. %end;
  112.  
  113. ods html close;
  114.  
  115. %mend analyzeSASdsn;
  116.  
  117.  
  118. /* Edit the Path of the Output Report */
  119.  
  120. %let htmlfilepath=C:\out.html;
  121.  
  122. options nodate pageno=1 linesize=80 pagesize=60 mprint symbolgen;
  123.  
  124. data cake;
  125. input LastName $ 1-12 Age 13-14 PresentScore 16-17
  126. TasteScore 19-20 Flavor $ 23-32 Layers 34 ;
  127. datalines;
  128. Orlando 27 93 80 Vanilla 1
  129. Ramey 32 84 72 Rum 2
  130. Goldston 46 68 75 Vanilla 1
  131. Roe 38 79 73 Vanilla 2
  132. Larsen 23 77 84 Chocolate .
  133. Davis 51 86 91 Spice 3
  134. Strickland 19 82 79 Chocolate 1
  135. Nguyen 57 77 84 Vanilla .
  136. Hildenbrand 33 81 83 Chocolate 1
  137. Byron 62 72 87 Vanilla 2
  138. Sanders 26 56 79 Chocolate 1
  139. Jaeger 43 66 74 1
  140. Davis 28 69 75 Chocolate 2
  141. Conrad 69 85 94 Vanilla 1
  142. Walters 55 67 72 Chocolate 2
  143. Rossburger 28 78 81 Spice 2
  144. Matthew 42 81 92 Chocolate 2
  145. Becker 36 62 83 Spice 2
  146. Anderson 27 87 85 Chocolate 1
  147. Merritt 62 73 84 Chocolate 1
  148. ;
  149.  
  150. run;
  151.  
  152.  
  153. /*Eg. Analyze the dataset cake to report on all variables */
  154.  
  155. %analyzeSASdsn(cake,max,_ALL_);
  156.  
  157. /* Eg. Analyze the dataset cake to report on particular variable Age Layers only */
  158. %analyzeSASdsn(cake,max,Age Layers);

Report this snippet  

You need to login to post a comment.