|
The Frequently Asked Questions (FAQs) listed below are for SAS for Windows, Version 9 only. They
represent the many common questions our Computing Core receives from affiliated students and faculty. If you are a MPRC
faculty associate/affiliate or graduate student affiliate and require further assistance with SAS, please contact the
Center's Statistical Software Programmer, Yeats Ye. She is available,
by appointment, to meet with MPRC researchers to discuss and address specific problems having to do with SAS and other
statistical programming.
QUESTIONS
- 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.
- How do I check
syntax errors in my SAS program before I submit it?
- How do I reorder
variables' position in a SAS data set?
- How do I remove
duplicate observations from my data set?
- I have a huge data
set. Is there a quick way for me to sort my whole data out?
- How do I replace
missing values with the variable mean in my data set?
- How do I accumulate
a numeric variable to get a total amount (totfaminc) for each group (one
household id per record)?
- How do I create a
unique ID for my data set?
- How do I count
observations per subject in a data set?
- How do I count
words in SAS?
- I have a messy
string variable with people's names and addresses. How do I correct them?
- How do I add a mean
value of a variable back to my original data set?
- 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).
- 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?
- 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?
- I am using a very
huge data set in SAS for windows. How do I minimize space requirements?
- What is the
difference between One-to-Many and Many-to-One Matched Merge in SAS?
- I have two data
sets. I want to do a Matched Merge and output only consisting of variables
from both files. How do I do it?
- I have two data
sets. How do I do a Matched Merge and output consisting of variables in
file1 but not in file2, or in file2 but not in file1 (id--4 5 6 7)?
- I have two data
sets. How do I do a Matched Merge and output consisting of variables from
master file (id--1, 2, 3, 4, 5)?
- 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)?
- 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.
- 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?
- How do I rotate
data from long format to wide format?
- 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 ...;)?
- How do I use Proc
SQL to get mean(var), std(var), min(var), max(var), median(var) from a data
set?
- How do I create a
data set with observations =100, mean 0 and standard deviation 1?
- How do I randomly
sample a certain proportion of observations from a SAS dataset? For example,
10%.
- 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.
- Is there a simple
way to indicate an integer range in SAS?
- How do I know what
products in my computer are licensed? What is my site number and when will
my SAS expire?
- I have a file that
is an activity based file for each person throughout the
day. There is a start time and end time as well as a duration time that
shows 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?
- How do I create a
data set with a permanent variable value label (permanent format)?
- I have just
received a SAS data file (emp.sas7bdat) with a SAS format file
(formats.sas7bcat). How do I use them?
- How do I create a
flat text file from a SAS data set?
- How do I set ODS to
save my output in HTML format by default?
- How do I sort the
values of two related arrays?
-
I am using a large data set. How do I
eliminate the output in the output window?
-
How can I change the orientation for
printing in my SAS program?
-
I want to save paper while printing. How can I change the
font when printing my SAS code, output file, and log file?
- How can I generate
percentile ranks using SAS?
- 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?
- How do I direct
output a file from output window to a flat file?
- How do I
automatically output a single txt file that contains both LOG and LST files?
- I want to save
paper when printing the output file by skipping page breaks. What can I do?
- 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?
- 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?
- How do I convert a
character date value to a SAS date value?
- 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?
-
How do I create a categorical variable using
a variable's percentage?
-
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?
-
I need to create a comma separated
file (.csv) from a SAS data, what should I do?
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;
cards;
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
option NodupKey to remove
duplicate observations for By key variables only from your data set.
2. You can use FIRST. and LAST. variables in a data step to remove duplicate observations.
3. You can also use FIRST. and LAST. variables in a data step if you want to see how many
observations are duplicated or vice versa.
data temp1;
input id x y ;
cards ;
1 20 1
1 20 1
1 20 2
2 20 3
3 20 4
;
proc sort in=temp1 out=temp2 nodup;
by id;
proc print data=temp2 ;
title 'no duplicates for all variables';
run;
proc sort in=temp1 out=temp3 nodupkey;
by id;
proc print data=temp3 ;
title 'no duplicate for by variable only';
run;
proc sort in=temp1;
by id;
data nodup
set temp1;
by id;
if first.id then output;
run;
data no_dup dup;
set temp1;
by id;
if first.id then output no_dup;
else output 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 the
observations 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;
cards;
1 . 1 6 6
2 5 2 7 7
3 5 . 8 8
4 5 4 9 9
5 5 5 . 10
;
proc standard data=raw out=rep_mean replace;
var var1-var5;
run;
Output:
1 5 1 6.0 6
2 5 2 7.0 7
3 5 3 8.0 8
4 5 4 9.0 9
5 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 30000
1 2 20000
1 3 0
2 1 50000
2 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 totfaminc
1 3 0 50000
2 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 50
B 11 12 13 14 15
C 15 20 25 30 35
D 22 33 44 55 66
;
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;
cards;
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 then
totc=count;
output;
run;
proc print;
run;
Output:
id num count totc
1 1 1 3
1 2 2 3
1 1 3 3
2 1 1 2
2 2 2 2
3 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 9.1. For example:
data proper;
input name char20.;
name = propcase(name);
datalines;
jeff Rust
Amy 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(keep=avg_height);
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;
data addavg;
merge sashelp.class new;
by sex;
drop _type_ _freq_;
run;
Output:
| Name |
Sex |
Age |
Height |
Weight |
Weight height |
n_height |
| Joyce |
F |
11 |
51.3 |
50.5 |
61.7500 |
8 |
| Yeats |
F |
12 |
59.8 |
84.5 |
61.7500 |
8 |
| Louise |
F |
12 |
56.3 |
77.0 |
61.7500 |
8 |
| Alice |
F |
13 |
56.5 |
84.0 |
61.7500 |
8 |
| Barbara |
F |
13 |
65.3 |
98.0 |
61.7500 |
8 |
| Carol |
F |
14 |
62.8 |
102.5 |
61.7500 |
8 |
| Judy |
F |
14 |
64.3 |
90.0 |
61.7500 |
8 |
| Janet |
F |
15 |
62.5 |
112.5 |
61.7500 |
8 |
| Mary |
F |
15 |
66.5 |
112.0 |
61.7500 |
8 |
| Thomas |
M |
11 |
57.5 |
85.0 |
62.7636 |
11 |
| James |
M |
12 |
57.3 |
83.0 |
62.7636 |
11 |
| John ye |
M |
12 |
59.0 |
99.5 |
62.7636 |
11 |
| Robert |
M |
12 |
64.8 |
128.0 |
62.7636 |
11 |
| Jeffrey |
M |
13 |
62.5 |
84.0 |
62.7636 |
11 |
| Alfred |
M |
14 |
69.0 |
112.5 |
62.7636 |
11 |
| Henry |
M |
14 |
63.5 |
102.5 |
62.7636 |
11 |
| William |
M |
15 |
66.5 |
1.0 |
62.7636 |
11 |
| Ronald |
M |
15 |
67.0 |
133.0 |
62.7636 |
11 |
| Philip |
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" library
proc datasets library=work memtype=data kill;
run;
quit;
2. Delete some temporary files from "WORK" library
proc datasets library=work memtype=data;
delete test1 test2;
*deletes datasets test1 & test2 and keeps the rest;
run;
proc datasets library=work memtype=data;
save test3;
*saves test3 and deletes the rest;
run;
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 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 $;
cards;
| Barry |
Sandy |
| Bill |
Sue |
| Ellen |
Sue |
| John |
Sue |
;
proc sort ;
by teacher;
data d3;
input teacher $ room;
cards;
Sandy 101
Sue 103
;
proc sort ;
by teacher;
data combine1t3 ;
merge d1 d3 ; by teacher ;
run;
data combine3t1 ;
merge d3 d1; by teacher ;
run;
Output(merge d1 d3):
| stud |
teacher |
room |
| Barry |
Sandy |
103 |
| Bill |
Sue |
101 |
| Ellen |
Sue |
101 |
| John |
Sue |
101 |
Output(merge d3 d1):
| teacher |
room |
stud |
| Sandy |
103 |
Barry |
| Sue |
101 |
Bill |
| Sue |
101 |
Ellen |
| Sue |
101 |
John |
18. Q: I have two data sets. I want to do a Matched Merge and output only consisting of variables
from both files. How do I do it?
A: In SAS, there is a Boolean flag called IN= variable. It is used for One-to-Many merge or Many-to-One 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 var name $;
cards;
1 john
2 joe
3 bill
4 bob
5 sandy
;
data file2;
input var state $;
cards;
1 MD
2 NY
3 VA
6 NJ
7 NC
;
data in_both;
merge file1(in=infile1) file2(in=infile2);
by var;
if infile1= infile2;
run;
Output:
1 john MD
2 joe NY
3 bill VA
19. Q: I have two data sets. How do I do a Matched Merge and output consisting of variables 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 var;
if infile1 ne infile2;
run;
Output:
4 bob
5 sandy
6 NJ
7 NC
20. Q: I have two data sets. How do I do a Matched Merge and output consisting of variables from
master file (id--1, 2, 3, 4, 5)?
A: data in_both;
merge file1(in=a) file2(in=b);
by var;
if a;
run;
proc print;
run;
Output:
1 john MD
2 joe NY
3 bill VA
4 bob
5 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 as
select 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;
Cards;
1 1
2 2
3 3
4 4
;
data data2;
Input id y;
Cards;
1 5
2 6
3 7
4 8
;
Data data3;
Input id z;
Cards;
1 9
2 10
3 11
4 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. 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 in:
cd c:\data
fdause class
save 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;
cards;
1 1 100 200
2 1 300 400
2 2 500 600
3 1 700 800
3 2 900 1000
3 3 1100 1200
4 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;
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;
run;
Output:
| tucaseid |
perlr1 |
perlr2 |
perlr3 |
perlr4 |
earv1 |
earv2 |
earv3 |
earv4 |
| 1 |
100 |
0 |
0 |
200 |
0 |
0 |
0 |
| 2 |
300 |
500 |
0 |
0 |
400 |
600 |
0 |
0 |
| 3 |
700 |
900 |
1100 |
0 |
800 |
1000 |
1200 |
0 |
| 4 |
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;
cards;
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;
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;
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 the
day. 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;
cards;
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;
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 agefmt
1-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;
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;
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.
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
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.;
cards;
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 variable
2. Use HMS function to convert them to seconds
3. 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);
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. ;
cards;
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?
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;
|