*using the First Last varialbes of group;
*first.group=last.group means there is only one record within that group;
data single dup;
set temp;
by group;
if first.group and last.group then output single;
else output dup;
run;
My notes for Statistic related topics, such as SAS, STATA programming. Other topics software related topics such as Latex may also be included.
Labels
- SAS (6)
Showing posts with label SAS. Show all posts
Showing posts with label SAS. Show all posts
Saturday, August 14, 2010
find duplicated data and put them into a different file
output a data set to a txt file
Output a SAS data set
data _null_;
set lib.zjl;
file 'drive:\path\file.txt' notitles noprint;
put var1 var2 var3 ... varn;
run;
data _null_;
set lib.zjl;
file 'drive:\path\file.txt' DLM=',';
put var1 var2 var3 ... varn;
run;
data _null_;
set lib.zjl;
file 'drive:\path\file.txt' DLM='09'x;
put var1 var2 var3 ... varn;
run;
data _null_;
set lib.zjl;
file 'drive:\path\file.txt' notitles noprint;
put var1 var2 var3 ... varn;
run;
data _null_;
set lib.zjl;
file 'drive:\path\file.txt' DLM=',';
put var1 var2 var3 ... varn;
run;
data _null_;
set lib.zjl;
file 'drive:\path\file.txt' DLM='09'x;
put var1 var2 var3 ... varn;
run;
use subset of observations in SAS procedures
*Limiting Observations
* Read from a raw data file;
filename myfile 'c:\SASfiles\temp\myfile';
Data temp;
infile myfile obs=100;
input ...;
run;
*printing data;
proc print data=temp (obs=10);
proc print data=temp (firstobs=20 obs=50);
*copy to other data set;
data temp2;
set temp (obs=10);
... other statements...
run;
*frequency test;
proc freq data=temp (obs=50);
var ...your variables...
run;
* Read from a raw data file;
filename myfile 'c:\SASfiles\temp\myfile';
Data temp;
infile myfile obs=100;
input ...;
run;
*printing data;
proc print data=temp (obs=10);
proc print data=temp (firstobs=20 obs=50);
*copy to other data set;
data temp2;
set temp (obs=10);
... other statements...
run;
*frequency test;
proc freq data=temp (obs=50);
var ...your variables...
run;
Friday, August 13, 2010
Missing Values in SAS
Find out missing value of a vairable
http://www2.sas.com/proceedings/sugi31/025-31.pdf
Character missing values are represented by a single blank enclosed in quotes (‘ ‘).
Numeric missing values are represented by a single period (.).
Special numeric missing values are represented by a single period followed by a single letter or an underscore (for example .A, .S, .Z, ._).
IF VALUE <=.Z THEN PUT “*** Value is missing”;
the order of missing variable:: negative number > .z > .(letters) > . > _
http://www2.sas.com/proceedings/sugi31/025-31.pdf
Character missing values are represented by a single blank enclosed in quotes (‘ ‘).
Numeric missing values are represented by a single period (.).
Special numeric missing values are represented by a single period followed by a single letter or an underscore (for example .A, .S, .Z, ._).
IF VALUE <=.Z THEN PUT “*** Value is missing”;
the order of missing variable:: negative number > .z > .(letters) > . > _
remove labels formats informats from a SAS data set
The following example of code removes all labels and formats for the dataset temp in the directory referenced by the LIBNAME WORK:
proc datasets lib=work nolist;
modify temp;
attrib _all_ label=''; *Remove labels;
format _all_; *Remove formats;
informat _all_; *Remove informats;
quit;
run;
It can also be done in the data step:
data temp;
set temp;
attrib _all_ label=''; *remove labels;
format _all_; *remove formats;
informat _all_; *remove informats;
run;
proc datasets lib=work nolist;
modify temp;
attrib _all_ label=''; *Remove labels;
format _all_; *Remove formats;
informat _all_; *Remove informats;
quit;
run;
It can also be done in the data step:
data temp;
set temp;
attrib _all_ label=''; *remove labels;
format _all_; *remove formats;
informat _all_; *remove informats;
run;
Thursday, August 12, 2010
SAS character to number or date
Character to number:: input( charvar, 6.0);
number to Character:: put(numvar, 4.0);
character to date:: input(charvar, mmddyy10.);
number to date:: mdy(mon, date, year);
remove specific character from a string:: compress(charvar, '()- '); *remove the (, ), -, and white space from the variable.
/* Program 1:: Convert num to char and char to num */
data temp1;
input numvar charvar $4.;
/* convert character to numeric */
numvar_n=input(charvar, 4.);
/* convert numeric to character */
char_n=put(numvar,4.0);
cards;
123 7691
789 0306
011 8729
;;
proc print data=temp1;
run;
/*program 2 character to date and number to date*/
data temp2;
input date_char mon day year;
/* convert character to date */
date1=input(date_char, mmddyy10.);
/* convert numeric to date */
date2=mdy(mon,day,year);
cards;
10/01/1999 10 01 1999
12/24/2003 12 24 2003
07/01/2001 7 1 2001
;;
proc print data=temp2;
run;
*program 3 remove the white space from a date variable;
data temp2;
input date_char mon day year;
/* convert character to date */
date1=input(compress(date_char), mmddyy10.);
*here the compress without the second parameter;
*by default it is the white space;
/* convert numeric to date */
date2=mdy(mon,day,year);
cards;
*instead of 10/01/1999, this format is 10/ 1/1999 using a space instead of 0;
10/ 1/1999 10 01 1999
12/24/2003 12 24 2003
7/ 1/2001 7 1 2001
;;
proc print data=temp2;
run;
number to Character:: put(numvar, 4.0);
character to date:: input(charvar, mmddyy10.);
number to date:: mdy(mon, date, year);
remove specific character from a string:: compress(charvar, '()- '); *remove the (, ), -, and white space from the variable.
/* Program 1:: Convert num to char and char to num */
data temp1;
input numvar charvar $4.;
/* convert character to numeric */
numvar_n=input(charvar, 4.);
/* convert numeric to character */
char_n=put(numvar,4.0);
cards;
123 7691
789 0306
011 8729
;;
proc print data=temp1;
run;
/*program 2 character to date and number to date*/
data temp2;
input date_char mon day year;
/* convert character to date */
date1=input(date_char, mmddyy10.);
/* convert numeric to date */
date2=mdy(mon,day,year);
cards;
10/01/1999 10 01 1999
12/24/2003 12 24 2003
07/01/2001 7 1 2001
;;
proc print data=temp2;
run;
*program 3 remove the white space from a date variable;
data temp2;
input date_char mon day year;
/* convert character to date */
date1=input(compress(date_char), mmddyy10.);
*here the compress without the second parameter;
*by default it is the white space;
/* convert numeric to date */
date2=mdy(mon,day,year);
cards;
*instead of 10/01/1999, this format is 10/ 1/1999 using a space instead of 0;
10/ 1/1999 10 01 1999
12/24/2003 12 24 2003
7/ 1/2001 7 1 2001
;;
proc print data=temp2;
run;
Subscribe to:
Posts (Atom)