SAS

/* convert numeric to character */
char_year=put(year,4.0);

/* convert character to numeric */
char1='1234.56';                                                         
num1=input(char,7.);

 

PROC MIXED manual for mixed models

https://drive.google.com/file/d/1ls8DQj3rLo20xW2LroADhzw56Dhnudc_/view

proc sql;
create table fullsample2 as
select *,
count(flag) as N_students
from fullsample
group by group_school;
run;

proc export data=final
outfile='C:\Users\raw data 2022 02 14\Pairwise Analysis2.xlsx'
dbms=xlsx replace;
sheet='final';
run;

 

PROC SQL

proc sql;
create table asdf5 as
select *,
count(casenumber) as duplicN,
var(date_var_n) as date_var_n_var,
from same_ref_date_data
group by casenumber , project_year;
run;

Read SAS data without loading formats

options NOFMTERR;

 

proc transpose data=asdf1 out=asdf1T;
id response_value ;
var _all_;
run;

 

Functions

When there is a space in between the first part of the value and the second part of the value:

item1=scan(item,1,' ');

item2=scan(item,2,' ');

run;

 

PROC SQL

This adds columns of new variables to the existing dataset:

proc sql;
create table groupvar2 as
select *,
max(_1_level2var) as _1_level2var_,
max(_1_level1var) as _1_level1var_
from groupvar;
run;

This creates a new dataset that contains the result:

proc sql ;
CREATE TABLE X AS
SELECT AVG(DateModified)
FROM access1.table1;
run;

 

 

Capitalization

lowcase, upcase, popcase

 

PROC MEANS STACK DATA OPTION
proc means data=kaz5 STACKODSOUTPUT ;
class treat;
var Z_GRADE_AVERAGE STEM ;
ods output summary=niko1;
run;

http://support.sas.com/documentation/cdl/en/proc/65145/HTML/default/viewer.htm#p17h6q7ygvkl1sn13qzf947dundi.htm

 

Take out strings at the beginning of character variables (and leave numeric)

digit=anydigit(studentID);
val=substr(studentID,digit);

 

Take the first and last observations (horizontally)

data one;
input Jan Feb March April;
cards;
11 . 32 .
. 33 22 12
12 . . .
;

data new(drop=i);
set one;
flag='0';
array vars(*) _numeric_;
retain flag;
do i = 1 to dim(vars);
if vars(i) ne . and flag ne '1' then do;
first=vars(i);
flag='1';
first_month=vname(vars(i));
end;
else if vars(i) ne . then do;
second=vars(i);
last_month=vname(vars(i));
end;
end;
if second=. then do;
second = first;
last_month=first_month;
end;

run;

proc print;
run;

 

Cronbach Alpha using PROC CORR

Cronbach Coefficient Alpha

 

Essential SAS techniques

Identify duplicate rows using PROC SQL.  This returns the number of duplicative rows per ID (e.g., email address)

proc sql;
create table all2 as
select *,
count(email) as duplic_count
from all
group by email;
run;

 

Export SAS datasets into Excel

proc export data=sashelp.class

outfile='C:\sastest\class.xlsx'

dbms=xlsx replace;

sheet='Class';

run;

 

PROC SQL and how to normalize/adjust weights (such that the sum of weights = the number of cases in the sample).  This runs in any PCs as it uses sashelp.class (the dataset that comes with SAS installation).

proc sql;
create table newdata1 as
select *,
weight * (count(weight)/Sum(weight)) as Adjusted_weight1
from sashelp.class;
proc sql;
create table newdata2 as
select *,
weight * (count(weight)/Sum(weight)) as Adjusted_weight2
from newdata1
group by sex;

 

Functions

Add _ in between empty spaces in the values (Thanks Sharon):

County=TRANWRD(trim(County)," ","_");

new=substr(x,2,5);

These concatenate variables but keep a space between variabels.

subgroup=compbl(school_level||categorydesc||outcomesubstance2);

subgroup=catx(' ',school_level,categorydesc,outcomesubstance2);

 

Replace a character with another character (or in this case, I'm replacing "_" with nothing (""), which means I'm removing "_".

Affilication=TRANWRD(Affilication,"_","") ;

Concatenate variables SAS LINK .  First variable specified (x1 in this example) will be used to separate variables (e.g., if x1=" ", a blank will be used to separate values from x2 and x3).

x=catx(x1 x2 x3);

Quickly create a specific date variable:

data temp;
x='01APR2016'D;
FORMAT x date9. ;run;
run;

Create a date variable using MDY

FORMAT Query_date date9. ;
Query_date=MDY(1,23,2016);

Create a text file with variables

data _null_;set kaz2t2;
blank=' ';
file "c:\temp\example.txt"; /*you can change this*/
put
(syntax) (500.0);
run;

 

Remove labels

My example:

proc datasets library=work nolist;
modify all;
attrib _all_ label='';
quit;

Easy Way to Remove All Variable Labels in SAS

 

Make all values in text variables expressed in capital letters.

http://support.sas.com/kb/39/525.html