unknown questions in sas

31
ANSWERS 1. Q: How do I change default SAS System user profile folder in SAS 9.0? For example, I want to set the working directory to c:\ mysas. A: The best way to do this is to change the -sasuser parameter in the sasv9.cfg file. Just set this in sasv9.cfg from your computer and you are ready to go. -sasuser "c:\mysas" 2. Q: How do I check syntax errors in my SAS program before I submit it? A: 1. Add "Option Obs=0; NoReplace;" on the top of your SAS program and submit your SAS program. 2. Check your Log window and reset System Options back to "Obs=max; Replace;" if there is not any errors in your SAS program. 3. Submit your SAS program again. 3. Q: How do I reorder variables' position in a SAS data set? A: You can add either Length or Retain statement before a Set statement in a data step. For example: data a; input a1 b1 c1 a2 b2 c2 a3 b3 c3; datalines; 1 2 3 4 5 6 7 8 9 ; data b; length a1-a3 3. b1-b3 3. c1-c3 3. ; set a; run;

Upload: sairam-ramanathan

Post on 28-Jan-2016

19 views

Category:

Documents


1 download

DESCRIPTION

Useful SAS question for Base SAS a00-211

TRANSCRIPT

Page 1: Unknown Questions in Sas

ANSWERS

1. Q: How do I change default SAS System user profile folder in SAS 9.0? For example, I want to set the working directory to c:\mysas.

A: The best way to do this is to change the -sasuser parameter in the sasv9.cfg file. Just set this in sasv9.cfg from your computer and you are ready to go.

-sasuser "c:\mysas"

2. Q: How do I check syntax errors in my SAS program before I submit it?

A:1. Add "Option Obs=0; NoReplace;" on the top of your SAS program and submit your SAS program.2. Check your Log window and reset System Options back to "Obs=max; Replace;" if there is notany errors in your SAS program.3. Submit your SAS program again.

3. Q: How do I reorder variables' position in a SAS data set?

A: You can add either Length or Retain statement before a Set statement in a data step.

For example:data a; input a1 b1 c1 a2 b2 c2 a3 b3 c3; datalines;1 2 3 4 5 6 7 8 9;

data b; length a1-a3 3. b1-b3 3. c1-c3 3. ;set a; run;

data c; retain a1-a3 b1-b3 c1-c3;set a;run;

4. Q: How do I remove duplicate observations from my data set?

A:1. You can use Proc Sort with option Nodup to remove duplicate observations for all variables or with

Page 2: Unknown Questions in Sas

option NodupKey to remove duplicate observations for By key variables only from your data set.2. Dupout is a new option in Proc Sort. It names a SAS data set that will contain the duplicaterecords eliminated from the data set.3. You can also use FIRST. and LAST. variables in a data step to remove duplicate observations.

data temp1;input id x y ;cards ;1 20 11 20 11 20 22 20 33 20 4;

proc sort data=temp1 out=temp2 nodup;by id;run;

proc print data=temp2 ;title 'no duplicates for all variables';run;

proc sort data=temp1 out=temp3 nodupkey dupout=dropped;by id;run;

proc print data=temp3 ;title 'no duplicate for by variable only';run;

proc print data=dropped;title 'new data set Dropped lists 2 observations with duplicate key values were deleted from the data set Temp1';run;

proc sort data=temp1;by id;run;

data nodup;set temp1;by id;if first.id then output;run;

Page 3: Unknown Questions in Sas

proc print data=nodup;run;

data nodup dup;set temp1;by id;if first.id then output nodup;else output dup;run;

proc print data=nodup;run;

proc print data=dup;run;

5. Q: I have a huge data set. Is there a quick way for me to sort my whole data out?

A: Usually, a general rule-of-thumb for calculating sort space requires three times the space of the dataset.1. The Tagsort option is very good on large datasets where key is small. 2. Use the Noequals option on PROC SORT if you do not want to keep that order the same as theobservations that were in the input data.

6. Q: How do I replace missing values with the variable mean in my data set?

A: The easiest way is to use Proc Standard with Replace option to replace all missing values with the variable mean. For example:

data raw ; input var1-var5; datalines;1 . 1 6 62 5 2 7 73 5 . 8 84 5 4 9 95 5 5 . 10; proc standard data=raw out=rep_mean replace; var var1-var5; run;

Output:1 5 1 6.0 62 5 2 7.0 73 5 3 8.0 8

Page 4: Unknown Questions in Sas

4 5 4 9.0 95 5 5 7.5 10

7. Q: How do I accumulate a numeric variable to get a total amount (totfaminc) for each group (one hhid per record)?

A: You can use First. and Last. variables in a data step to do it.

data test;input hhid pn faminc;datalines;1 1 300001 2 200001 3 02 1 500002 2 0;proc sort;by hhid;run ;data new;set test;by hhid;if first.hhid then totfaminc=0;totfaminc+faminc;if last.hhid then output;drop pn;run;

Output:hhid pn faminc totfaminc1 3 0 500002 2 0 50000

8. Q: How do I create a unique ID for my data set?

A: You can either use Sum statement (id+1;) or system variable "_n_" to do so.

data demo;id+1;input id $ v1-v5;cards ;A 10 20 30 40 50B 11 12 13 14 15C 15 20 25 30 35D 22 33 44 55 66

Page 5: Unknown Questions in Sas

;data new;set sashelp.class;id=_n_;run;

9. Q: How do I count observations per subject in a data set?

A: Use SUM statement and FIRST. variable to count observations per subject. For example:

data temp; input id num; datalines;1 1 1 2 1 1 2 1 2 2 3 1 ; proc sort data=temp; by id;

data temp1; set temp; by id; count+1; if first.id then count=1; run;proc sort;by id descending count;run;data temp2;set temp1;by id; retain totc;if first.id thentotc=count;output;run;

proc print;run;

Output:

Page 6: Unknown Questions in Sas

id num count totc1 1 1 31 2 2 31 1 3 32 1 1 22 2 2 23 1 1 1

10. Q: How do I count words in SAS?

A: Use the function of count( ) in SAS v9 to simply count the number of sub-strings that occur in a string.

data a;x="good good study, day day up"y=count(x,'day');run;

11. Q: I have a messy string variable with people's names and addresses. How do I correct them?

A: It's very easy to do it with PROPCASE function in SAS V9. For example:

data proper;input name char20.;name = propcase(name);datalines;jeff RustAmy lee;proc print; run;

12. Q: How do I add a mean value of a variable back to my original data set?

A: 1. Use Proc Means to create a new data set to hold a mean value for the variable.2. Use a data step with two SET statements to add a mean value back to your original data set.

For example:proc means data=sashelp.class;var height;output out=new mean=avg_height;run;

data addavg;if _N_ =1 then set new;

Page 7: Unknown Questions in Sas

set sashelp.class ;run;

13. Q: How do I add mean values and total count of a categorical variable back to my original data set? For example: mean values of height for Sex (female and male).

A:1. Sort the data by Sex.2. Use Proc Means with BY to create a new data set to hold a mean and count values by Sex.3. Use a data step with MERGE statements to merge them by Sex.

proc sort data=ye.class;by sex;proc means data=ye.class;var height;by sex;output out=new mean=avg_height n =n_height;run;proc sort;by sex; run;data addavg;merge sashelp.class new;by sex;drop _type_ _freq_;run;

Output:

Name Sex Age Height Weight Weight height n_heightJoyce F 11 51.3 50.5 61.7500 8Yeats F 12 59.8 84.5 61.7500 8Louise F 12 56.3 77.0 61.7500 8Alice F 13 56.5 84.0 61.7500 8Barbara F 13 65.3 98.0 61.7500 8Carol F 14 62.8 102.5 61.7500 8Judy F 14 64.3 90.0 61.7500 8Janet F 15 62.5 112.5 61.7500 8Mary F 15 66.5 112.0 61.7500 8Thomas M 11 57.5 85.0 62.7636 11James M 12 57.3 83.0 62.7636 11John ye M 12 59.0 99.5 62.7636 11Robert M 12 64.8 128.0 62.7636 11Jeffrey M 13 62.5 84.0 62.7636 11Alfred M 14 69.0 112.5 62.7636 11Henry M 14 63.5 102.5 62.7636 11

Page 8: Unknown Questions in Sas

William M 15 66.5 1.0 62.7636 11Ronald M 15 67.0 133.0 62.7636 11Philip M 16 72.0 1.0 62.7636 11

14. Q: I have a program that I closed and forgot to save but I do have a log file from when I ran it. Can I take the text from the log file and make it a program again?

A: Yes, You can. In log window, hold ALT, move the cursor to your code and highlight it. Right click and select edit, then select copy. Go back to SAS Edit Window and paste it.

15. Q: I want to save hard drive space. How do I write a code to delete some or all temporary files once I do not need them any more?

A: You can manually delete all temporary files from "Work" library. You can also use the Datasets procedure with the Delete or Save statement to delete some of temporary files. The Kill option will delete all SAS files immediately after you submit the statement.

Example:1. Delete all temporary files from "Work" libraryproc datasets library=work memtype=data kill; run;quit;

2. Delete some temporary files from "Work" libraryproc datasets library=work memtype=data;delete test1 test2;*deletes datasets test1 & test2 and keeps the rest;quit;

proc datasets library=work memtype=data;save test3; *saves test3 and deletes the rest;quit;

16. Q: I am using a very huge data set in SAS for windows. How do I minimize space requirements?

A: When you are working with large data sets, you can do the following steps to reduce space requirements.

1. Split huge data set into smaller data sets.2. Clean up your working space as much as possible at each step.3. Use dataset options (keep= , drop=) or statement (keep, drop) to limit to only the variables needed. 4. Use IF statement or OBS = to limit the number of observations. 5. Use WHERE= or WHERE or index to optimize the WHERE expression to limit the number of

Page 9: Unknown Questions in Sas

observations in a Proc Step and Data Step. 6. Use length to limit the bytes of variables.7. Use _null_ dataset name when you don't need to create a dataset. 8. Compress dataset using system options or dataset options (COMPRESS=yes or COMPRESS=binary).9. Use SQL to do merge, summary, sort etc. rather than a combination of Proc Step and Data Step with temporary datasets.

17. Q: What is the difference between One-to-Many and Many-to-One Matched Merge in SAS?

A: One-to-Many merge is the same as a Many-to-One Matched Merge except the order of the variables in the new data set is different. For example:

data d1;input stud $ teacher $; datalines;

Barry SandyBill SueEllen SueJohn Sue

;proc sort ;by teacher;

run;

data d2;input teacher $ room;datalines;Sandy 101Sue 103;proc sort ;by teacher; run;

data combine1t2 ; merge d1 d2 ; by teacher ; run;

data combine2t1 ; merge d2 d1; by teacher ;

Page 10: Unknown Questions in Sas

run;

Output(merge d1 d2;):

stud teacher roomBarry Sandy 103Bill Sue 101Ellen Sue 101John Sue 101

Output(merge d2 d1;):

teacher room studSandy 103 BarrySue 101 BillSue 101 EllenSue 101 John

18. Q: I have two data sets. I want to do a Matched Merge and output only consisting of observations from both files. How do I do it?

A: In SAS, there is a Boolean flag called IN= variable. It is used for matched merge to track and select which observations in the data set from the merge statement will go to a new data set.

data file1;input id name $;datalines;1 John2 Joe3 Bill4 Bob5 Sandy;

proc sort data=file1;by id;run;

data file2;input id state $;datalines;1 MD2 NY3 VA

Page 11: Unknown Questions in Sas

6 NJ7 NC;

proc sort data=file2;by id;run;

data in_both;merge file1(in=infile1) file2(in=infile2);by id;if infile1= infile2;run;Output:1 John MD2 Joe NY3 Bill VA

19. Q: I have two data sets. How do I do a Matched Merge and output consisting of observations in file1 but not in file2, or in file2 but not in file1 (id--4 5 6 7)?

A:

data in_both;merge file1(in=infile1) file2(in=infile2);by id;if infile1 ne infile2;run;Output:4 Bob5 Sandy6 NJ7 NC

20. Q: I have two data sets. How do I do a Matched Merge and output consisting of observations from master file (id--1, 2, 3, 4, 5)?

A:

data in_both;merge file1(in=a) file2;by id;if a; run;

Page 12: Unknown Questions in Sas

proc print;run;

Output:1 john MD2 joe NY3 bill VA4 bob5 sandy

21. Q: I have two large data sets. How do I do a Matched Merge and output consisting of variables file1 but not in file2 (id--4, 5)?

A: Both Data Step with Merge statement and Proc SQL are OK, but Proc SQL has a higher efficiency to do it for large data set.

data not_in_a;merge file1(in=x) file2(in=y);by id;if not y;run;

proc sql; create table not_in_a asselect id from file1 except select id from file2; quit;

22. Q: How do I merge 10 data sets with a common variable and the same prefix data name? For example: data1 to data10 with common variable of ID.

A: The best way is to use a macro.

options mprint;data data1;input id x;datalines;1 12 23 34 4;

Page 13: Unknown Questions in Sas

data data2;input id y;datalines;1 52 63 74 8;data data3;input id z;datalines;1 92 103 114 12;....%macro mymerge (n); Data merged; Merge %do i = 1 % to &n; data&i %end; By id; Run; %mend;

%mymerge(10)

23. Q: I have SAS and STATA but I do not have Stat/Transfer program in my computer. Is there any easy way for me to convert my SAS data set to STATA?

A: Yes.

Method1: There is a Stata user-written (Ado-file) command “usesas” created by Dr. Dan Blanchette. The command uses SAS to run SAS macro savastata.sas and load the SAS dataset into Stata's memory if you have SAS installed in your computer. The user can decide to save the data as a Stata data set using Stata's “save” command after that.

For example:

1. Use Stata's command "ssc install" to download and install it.ssc install usesas

2. Use "usesas" command to loads a SAS data set from your computer into Stata's memory.usesas using “c:\data\yoursasdat.sas7bdat”

Page 14: Unknown Questions in Sas

3. Save your Stata data if needed.save datname

Method2:

In SAS, create a transport format file.libname out XPORT "c:\data\class.xpt";data out.class;set sashelp.class;run;

In STATA, use command "fdause" to read the transport format file into Stata's memory:cd c:\datafdause classsave class

24. Q: How do I rotate a data from long format to wide format?

A: Restructure data set from long format to wide format with arrays.

data x;input tucaseid tulineno perlr earv;datalines;1 1 100 2002 1 300 4002 2 500 6003 1 700 8003 2 900 10003 3 1100 12004 1 1300 1400 ;proc sort;by tucaseid ;run;

data y ;set x;by tucaseid ;retain perlr1-perlr4 earv1-earv4 i;array ary_perlr(4) perlr1-perlr4;array ary_earv(4) earv1-earv4;

if first.tucaseid then do;do i = 1 to 4;ary_perlr(i)=0;

Page 15: Unknown Questions in Sas

ary_earv(i)=0;end;i=1; end;

ary_perlr(i)=perlr;ary_earv(i)=earv;i=i+1;if last.tucaseid then output;

drop perlr earv i tulineno;run;

Output:

tucaseid perlr1 perlr2 perlr3 perlr4 earv1 earv2 earv3 earv41 100 0 0 200 0 0 02 300 500 0 0 400 600 0 03 700 900 1100 0 800 1000 1200 04 1300 0 0 0 1400 0 0 0

25. Q: My string variable MEMO contains two quotes (it's 'ok'). What should I do if I want to use an if/then statement to check the value of X (if MEMO = it's 'ok' then ...;)?

A: Check the value of MEMO using if/then statement as follows: if MEMO = "it's 'ok'" then y=1;

26. Q: How do I use Proc SQL to get mean(var), std(var), min(var), max(var), median (var) from a data set?

A: Median() function is not a "summary" function in PROC SQL. But it is possible for you to calculate MEDIAN with more labor job using PROC SQL.data a; input age; datalines;1 2 3 4 6 ; Proc sql; Select mean(age), std(age), min(age), max(age) from a; Select avg(median) as median from (select x.age as median from a as x, a as y group by x.age having sum(sign(x.age-y.age)) in (1,0,-1)); Quit;

Page 16: Unknown Questions in Sas

27. Q: How do I create a data set with observations =100, mean 0 and standard deviation 1?

A: data one;do i = 1 to 100;num = 0 + rannor(1) * 1; output; end; run;

proc means data = one mean stddev; var num; run;

28. Q: How do I randomly sample a certain proportion of observations from a SAS dataset? For example, 10%.

A: In the DATA step, include the line to select approximately 10% of the observations from the original data. if ranuni(0)<=.1 ;

You can also use Proc Surveyselect to create a simple random sampling size. For example, sampling size 10.

proc surveyselect data=sashelp.class method=srs n=10 out=Sample10; run;

29. Q: How do I create a summarized report without creating a new variable? For example: I have an AGE variable and I want to generate a summarized report that shows the number of people in different groups.

A:1. Create a user defined format for the group. 2. In Proc Freq, when the format is applied to a variable on the TABLE statement, the formatted value will be used for the distribution.

Proc format; Value fmtnum . = 'missing'Low -<0 = 'negative'0 = 'zero'0 <- high = 'positive';Run;

Proc freq;

Page 17: Unknown Questions in Sas

var sex; format sex sexfmt.; run;

30. Q: Is there a simple way to indicate an integer range in SAS?

A: Yes, in SAS version9, IN operator accepts integer ranges more easily than before. For example: you can use "age in (11, 13:19)" instead of "if age =11 or 13 <= age <=19"

31. Q: How do I know what products in my computer are licensed? What is my site number and when will my SAS expire?

A: Proc setinit; tells you what you have currently valid licenses and site number, and defined products in your session.

Proc setinit; run ;

32. Q: I have a file that is an activity based file for each person throughout theday. There is a start time and end time as well as a duration time that show how many minutes they were doing the activity. I need to create a file which has a 1440 observation for each person for every minute of the day. How do I create a time serial data?

A: There are several ways to do it. If your START TIME and END TIME are SAS time values, you can easily create "startmin" variable using "starttime" divided by 60 and create "endmin" variable using "endtime" divided by 60, and then create your time serial data. Otherwise, you can just use duration time variable to do it.

data a; input id activity_code starttime endtime; datalines;1 1 1 8 1 2 9 17 1 3 18 24 2 1 1 9 2 5 10 18 2 3 19 24 ;

data b; set a; hrs=starttime; do until(hrs=endtime); hrs+1; count=hrs-starttime; output;

Page 18: Unknown Questions in Sas

end; run; %p

33. Q: How do I create a data set with permanent variable value label (permanent format)?

A. 1. Create a permanent format using option of "library=project" in proc format statement.2. Use "Options FMTSEARCH = (project);" to tell SAS where to look for the format. 3. Accesses a permanent format using format statement.

/*myformat.sas -- Creating a permanent format*/Libname project 'C:\';proc format library=project;value $ sexfmt'M'='male''F '='female';value agefmt1-17 ='17 and under'18-high ='18 and up';run;

/*mysasfile.sas--Accessing a permanent format*/Options FMTSEARCH = (project); /*Tell SAS where to look for the format */ libname project 'c:\';

data ye.perm_format;set sashelp.class;format sex $sexfmt. age agefmt.;run;proc print;run;

34. Q: I have just received a SAS data file (emp.sas7bdat) with a SAS format file (formats.sas7bcat). How do I use them?

A: Save both of them in the same directory in your computer. For example, C:\. Write a SAS program as below and submit it.

libname mylib "c:\"; libname library "c:\"; options fmtsearch = mylib;

Page 19: Unknown Questions in Sas

proc means data=mylib.auto; run;

35. Q: How do I create a flat text file from a SAS data set?

A: You can use a Stat/Transfer program to convert it. You can also use Procure (Proc export and Proc printto) or File and Put statements in a data step to create it.

filename rawdat 'c:\output.txt' ; data _null_; set sashelp.class ; file rawdat ; put name age; run ;

36. Q: How do I set ODS to save my output in HTML format by default?

A: You can go to SAS preference screen to reset settings available to tell SAS to save all Data step and Proc output in HTML format.

1. Click the Tools option, 2. Click the Options option, 3. Click the Preferences... option,4. Click the Results tab, 5. For HTML output, check the Create HTML box.

37. Q: How do I sort the values of two related arrays?

A: You can either use Do loop with two arrays or CALL SORTN to do it.

data temp;x=3 ;y=1 ;z=2 ; value_x=100;value_y=300;value_z=200;run ;

data b;set temp;array ary1(3) x y z;array ary2(3) value_x value_y value_z;max=0;do i = 1 to 3;if ary1(i) > max then do;max= ary1(i) ; maxother= ary2(i) ; end;

Page 20: Unknown Questions in Sas

else do;t=max; ary1(i-1)=ary1(i);ary1(i)=t; tother=maxother; ary2(i-1)=ary2(i); ary2(i)=tother; end;end;keep x y z value_x value_y value_z;run;

data sortarray; set temp; CALL SORTN (x, y, z, value_x, value_y, value_z); run;

38. Q: I am using a large data set. How do I eliminate the output in output window?

A: Add the following statement in your code to make "No output destinations active". ODS listing close;

39. Q: How can I change the orientation for printing in my SAS program?

A: Add the follow command in your SAS program to control the printer's orientation setting.

dm 'dlgprtsetup orient=landscape nodisplay';

40. Q: I want to save paper while printing. How can I change the font when printing my SAS code, output file, and log file?

A. The default font for printing SAS code, output file, and log file is 10. You can use option SYSPRINTFONT to set it.

OPTIONS SYSPRINTFONT='SAS Monospace' 6;proc print data=sashelp.class; run;

41. Q: How can I generate percentile ranks using SAS?

A: proc rank groups=100 out=dataset; var x y; ranks prv1 prv2; run;

42. Q: How do I generate a directory file with total observations, total variables, file size and the last time the file was modified for a defined library name?

A: Use proc datasets to check information of total observations, total variables, file size and the last time the file was modified for a defined "library" from log window.

Page 21: Unknown Questions in Sas

proc datasets library=mylib mt=data details; run; quit;

Use ODS to save them to a new data set:

ods output members=dir_mylib; proc datasets library=mylib mt=data details; run; quit; ods output close;

43. Q: How do I direct outputting a file from output window to a flat file?

A: You can use Proc Printto to automatically route output to a file. proc printto print='c:\auto.lst' new; .... run;

44. Q: How do I automatically output a single txt file that contains both LOG and LST files? A: filename proj1 'c:\mysas\output\proj1.txt'; proc printto log= proj1 print= proj1 new;

*begging your program here*/ data test; set sashelp.class; proc reg; model weight = height; run; *end your program here*/

proc printto; run;

45. Q: I want to save paper when printing output file by skipping page breaks. What can I do?

A: To turn off the page break setting form output window, use system options.

To turn off the page break set this option.....options formdlim=' '; a blank

To turn page breaks back on options formdlim=''; no blank

Page 22: Unknown Questions in Sas

data new;time="11:25";hour=scan(time,1,':');minute=scan(time,2,':');sastime=hms(hour,minute,0);format sastime time5.;run;

46. Q: I have a character variable with regulate time value like 01:00:00 pm and 09:00:02 am in my SAS data set. I want to order 9am earlier than 1pm. What should I do?

A: You can use input function with SAS time format to convert it to SAS time value, and then sort by ascending.

data a; *input begin time8.; input begin $ 10.; datalines;01:00:00 am 01:00:00 pm 09:00:02 am 12:00:03 pm ; data b; set a; newtime=input(begin, time12.); proc sort; by newtime; proc print; run;

47. Q: I have a character variable with regulate time value like 04:30:01 or 23:00:00 in my SAS data set. I want to convert them to hours. How do I do that?

A:1. Use SCAN function to get how many hours, minutes and seconds from your character variable2. Use HMS function to convert them to seconds3. Convert seconds to hours

For example: data new; time="04:30:01"; hour=scan(time,1,':'); minute=scan(time,2,':'); se=scan(time,3,':'); sastime=hms(hour,minute,se);

Page 23: Unknown Questions in Sas

hr24=hms(hour,minute,se)/3600; convert24=hms(scan(time,1,':'),scan(time,2,':'),scan(time,3,':'))/3600; run;

48. Q: How do I convert character date value to a SAS date value?

A: You can use input function to do it.

data one; input chardate $8. ; datalines; 19600101 19591231 20000101 ; data two; set one; sasdate=input(chardate,yymmdd8.); run;

Output: chardate sasdate 19600101 0 19591231 -1 20000101 14610

49. Q: I have a character variable with mix regulate date value like 01mar99, 01 mar 99 and 01-mar-1999. I want to convert them to SAS date value. What should I do?

A: You can use input function with SAS date format to convert it to SAS date value.

SAS data set. data aa; input @1 var1 $ 20. ; datalines;01mar99 01 mar 99 01-mar-1999 ; data bb; set aa; statadate = input(var1,date11.) ; run;

50. Q: How do I create a categorical variable using a variable's percentage?

Page 24: Unknown Questions in Sas

A: You can use Proc Freq to generate a variable called PERCENT, merge it back to your original data, and then create a categorical variable using variable PERCENT.

proc freq data=ye.class; table age/out=agefreq(keep=age PERCENT); run; proc sort data=ye.class; by age; proc sort data=agefreq; by age; data all; merge ye.class(in=a) agefreq; by age; if a; PERCENT=int(PERCENT); if PERCENT > 20 then freqcat=1; else freqcat=0; run;

51. Q. I keep getting an error message "out of memory" when I use proc freq with two categorical variables to try to get a freq report. Why and how do I avoid this problem?

A: SAS stores all of the value combinations in memory during processing when you use Proc Freq. When there are too many levels of categorical variables they will use up many memories resulting in the error message "out of memory". In order to prevent this error, use Proc Sort with By statement and also include a By statement in your Proc Freq.

52. Q: I need to create a comma separated file (.csv) from a SAS data, what should I do?

A: A quick and easy way is using "Data _null_".

filename outfile 'c:\class.csv';data _null_; file outfile; set sashelp.class; put (_all_) (','); run;