# knitr::opts_chunk$set(echo = TRUE)
require(SASmarkdown)
## Loading required package: SASmarkdown
## sas, saslog, sashtml, and sashtmllog engines
## are now ready to use.
saspath <- "C:/Program Files/SASHome/SASFoundation/9.4/sas.exe"
sasopts <- "-nosplash -ls 75"
knitr::opts_chunk$set(engine='sashtml', engine.path=saspath,
engine.opts=sasopts, comment="")
PROC FORMAT;
VALUE LIKERT7_A
1,2,3 = "Disagree"
4 = "Neither Agree nor Disagree"
5,6,7 = "Agree"
RUN;
PROC FORMAT;
VALUE INCOME
LOW -< 20000 = "Low"
20000 -< 60000 = "Middle"
60000 - HIGH = "High";
RUN;
PROC FORMAT;
VALUE RACE
1 = "White"
2 = "Black"
OTHER = "Other";
RUN;
/*CREATING LABELS FOR CHARACTER VARIABLES*/
/*assigning labels to variable values*/
PROC FORMAT;
VALUE $GENDERLABEL
"M" = "Male"
"F" = "Female";
RUN;
/*Use defined format*/
DATA sample;
SET sashelp.class;
FORMAT sex GENDERLABEL. ;
RUN;
proc freq data=sample;
table sex;
run;
The FREQ Procedure
Cumulative Cumulative
Sex Frequency Percent Frequency Percent
-----------------------------------------------------------
Female 9 47.37 9 47.37
Male 10 52.63 19 100.00
LIBNAME format "C:\Users\hed2\Downloads\code-storage\code\format";
PROC FORMAT LIBRARY=format ;
VALUE $GENDER
"M" = "Male_new"
"F" = "Female_new";
RUN;
/*Use stored defined format way 1 */
OPTIONS FMTSEARCH=(format);
DATA sample;
SET sashelp.class;
FORMAT sex GENDER. ;
RUN;
proc freq data=sample;
table sex;
run;
The FREQ Procedure
Cumulative Cumulative
Sex Frequency Percent Frequency Percent
---------------------------------------------------------------
Female_new 9 47.37 9 47.37
Male_new 10 52.63 19 100.00
%INCLUDE 'C:\Users\hed2\Downloads\code-storage\code\format\format_test.sas';
DATA sample;
SET sashelp.class;
FORMAT sex GENDERsex. ;
RUN;
proc freq data=sample;
table sex;
run;
The FREQ Procedure
Cumulative Cumulative
Sex Frequency Percent Frequency Percent
----------------------------------------------------------------
Female_new2 9 47.37 9 47.37
Male_new2 10 52.63 19 100.00
Add formats to existing formatlib; seperate creating labels, useing format and labeling variables.
LIBNAME format "C:\Users\hed2\Downloads\code-storage\code\format";
PROC FORMAT LIBRARY=format ;
VALUE $GENDER
"M" = "Male_new"
"F" = "Female_new";
RUN;
/*add formats to existing formatlib*/
PROC FORMAT LIBRARY=format ;
VALUE $gendersextwo
"M" = "Male_new2_two"
"F" = "Female_new2_two";
RUN;
/*use stored defined format way 3 */
OPTIONS FMTSEARCH=(format);
DATA sample;
SET sashelp.class;
FORMAT sex GENDERsextwo. ;
RUN;
proc freq data=sample;
table sex;
run;
The FREQ Procedure
Cumulative Cumulative
Sex Frequency Percent Frequency Percent
--------------------------------------------------------------------
Female_new2_two 9 47.37 9 47.37
Male_new2_two 10 52.63 19 100.00
LIBNAME format "C:\Users\hed2\Downloads\code-storage\code\format";
PROC FORMAT LIBRARY=format ;
VALUE $GENDER
"M" = "Male_new"
"F" = "Female_new";
RUN;
/*use sas file to add format in batch*/
OPTIONS FMTSEARCH=(format);
DATA sample;
SET sashelp.class;
%INCLUDE 'C:\Users\hed2\Downloads\code-storage\code\format\use_format.sas';
RUN;
proc freq data=sample;
table sex;
run;
The FREQ Procedure
sex in class
Cumulative Cumulative
Sex Frequency Percent Frequency Percent
--------------------------------------------------------------------
Female_new2_two 9 47.37 9 47.37
Male_new2_two 10 52.63 19 100.00
/*User-defined formats cannot be edited. to create a macro by myself ; or proc freq then use excel*/;
PROC FORMAT ;
VALUE $GENDER
"M" = "Male_new"
"F" = "Female_new";
RUN;
proc format;
value $sex_f "F"="Female_f" other=[5.1];
run;
DATA sample;
SET sashelp.class;
FORMAT sex $sex_f. ;
RUN;
proc freq data=sample;
table sex;
run;
The FREQ Procedure
Cumulative Cumulative
Sex Frequency Percent Frequency Percent
-------------------------------------------------------------
Female_f 9 47.37 9 47.37
M 10 52.63 19 100.00
PROC FORMAT ;
VALUE $num
"M" = 1
"F" = 2;
RUN;
DATA sample_2;
SET sashelp.class;
sex_2 = input(put(sex ,num.),best.); /*Converting variable types and keep*/
RUN;
proc freq data=sample_2;
table sex_2;
run;
The FREQ Procedure
Cumulative Cumulative
sex_2 Frequency Percent Frequency Percent
----------------------------------------------------------
1 10 52.63 10 52.63
2 9 47.37 19 100.00
/*but we dont know the format name*/
/*PROC FORMAT FMTLIB;*/
PROC FORMAT ;
VALUE $GENDER
"M" = "Male_new"
"F" = "Female_new";
RUN;
proc format library = work.formats FMTLIB cntlout = cntlout;
select $GENDER;
run;
---------------------------------------------------------------------------
-
| FORMAT NAME: $GENDER LENGTH: 10 NUMBER OF VALUES: 2
|
| MIN LENGTH: 1 MAX LENGTH: 40 DEFAULT LENGTH: 10 FUZZ: 0
|
|--------------------------------------------------------------------------
|
|START |END |LABEL (VER. V7|V8
19SEP2022:14:34:57)|
|----------------+----------------+----------------------------------------
|
|F |F |Female_new
|
|M |M |Male_new
|
---------------------------------------------------------------------------
-
%macro formatd(data, var);
data temp;
set &data;
keep &var;
run;
/*original freq*/
ods output OneWayFreqs=sample_b ;
proc freq data=temp ;
table &var /missing ;
run;
ods output close;
/*delete format freq*/
data sample2;
set temp;
Format _all_;
run;
proc freq data=sample2 ;
table &var /missing out=sample_c ;
run;
/*select original variable code*/
proc sql noprint;
select name into :firstvar_b from dictionary.columns where libname='WORK' and memname='SAMPLE_B'
and varnum=2;
quit;
data sample_b2;
set sample_b;
Keep &firstvar_b ;
run;
/*select original variable label*/
proc sql noprint;
select name into :firstvar_c from dictionary.columns where libname='WORK' and memname='SAMPLE_C'
and varnum=1;
quit;
data sample_c2;
set sample_c;
Keep &firstvar_c ;
run;
/*merge variable code and label*/
data sample_bc;
set sample_b2 (RENAME=(&firstvar_b=new_b));
set sample_c2 (RENAME=(&firstvar_c=new_c));
run;
/*create format format and output*/
data sample_bc;
set sample_bc;
Original_format = CATS(new_c,"=","'",new_b,"'");
run;
proc print data=sample_bc noobs;
var Original_format;
run;
/*data _null_;*/
/* set sample_bc ;*/
/* file print;*/
/* putlog Original_format @@;*/
/* put Original_format @@;*/
/*run;*/
%mend formatd;
PROC FORMAT ;
VALUE $GENDER
"M" = "Male_new"
"F" = "Female_new";
RUN;
DATA sample;
SET sashelp.class;
FORMAT sex $GENDER. ;
RUN;
%formatd(sample, sex );
The FREQ Procedure
Cumulative Cumulative
Sex Frequency Percent Frequency Percent
---------------------------------------------------------------
Female_new 9 47.37 9 47.37
Male_new 10 52.63 19 100.00
The FREQ Procedure
Cumulative Cumulative
Sex Frequency Percent Frequency Percent
--------------------------------------------------------
F 9 47.37 9 47.37
M 10 52.63 19 100.00
Original_
format
F='Female_new'
M='Male_new'
%macro varnames (dat);
proc contents
data = &dat
noprint
out = data_info
(keep = name );
run;
proc print
data = data_info
noobs;
run;
%mend;
%varnames (sashelp.class)
NAME
Age
Height
Name
Sex
Weight