*Copyright @ www.mycsg.in;
Create a copy of an existing dataset
proc sql; create table class2 as select * from class; quit;
Copy Code
View Log
SAS Log
proc sql; create table class2 as select * from class; NOTE: Table WORK.CLASS2 created, with 19 rows and 5 columns. quit; NOTE: PROCEDURE SQL used (Total process time): real time 0.00 seconds cpu time 0.00 seconds
View Data
Dataset View
Add new variables or modify existing variables
proc sql; create table class2 as select *,age*12 as agemon,"Class 9" as class from class; quit;
Copy Code
View Log
SAS Log
proc sql; create table class2 as select *,age*12 as agemon,"Class 9" as class from class; NOTE: Table WORK.CLASS2 created, with 19 rows and 7 columns. quit; NOTE: PROCEDURE SQL used (Total process time): real time 0.01 seconds cpu time 0.00 seconds
Select only required variables
proc sql; create table ahw as select name,age,height,weight from class; quit;
Copy Code
View Log
SAS Log
proc sql; create table ahw as select name,age,height,weight from class; NOTE: Table WORK.AHW created, with 19 rows and 4 columns. quit; NOTE: PROCEDURE SQL used (Total process time): real time 0.00 seconds cpu time 0.01 seconds
View Data
Dataset View
Select only required records
proc sql; create table males as select * from class where sex="M"; quit;
Copy Code
View Log
SAS Log
proc sql; create table males as select * from class where sex="M"; NOTE: Table WORK.MALES created, with 10 rows and 5 columns. quit; NOTE: PROCEDURE SQL used (Total process time): real time 0.00 seconds cpu time 0.01 seconds
View Data
Dataset View
Sort the observations by values in one variable
proc sql; create table sorted as select * from class order by age; quit;
Copy Code
View Log
SAS Log
proc sql; create table sorted as select * from class order by age; NOTE: Table WORK.SORTED created, with 19 rows and 5 columns. quit; NOTE: PROCEDURE SQL used (Total process time): real time 0.01 seconds cpu time 0.00 seconds
View Data
Dataset View
Sort the observations by values in two variables
proc sql; create table sorted as select * from class order by age,height; quit;
Copy Code
View Log
SAS Log
proc sql; create table sorted as select * from class order by age,height; NOTE: Table WORK.SORTED created, with 19 rows and 5 columns. quit; NOTE: PROCEDURE SQL used (Total process time): real time 0.01 seconds cpu time 0.00 seconds
View Data
Dataset View
Sort the observations by values in descending order in one variable
proc sql; create table sorted as select * from class order by age desc; quit;
Copy Code
View Log
SAS Log
proc sql; create table sorted as select * from class order by age desc; NOTE: Table WORK.SORTED created, with 19 rows and 5 columns. quit; NOTE: PROCEDURE SQL used (Total process time): real time 0.01 seconds cpu time 0.00 seconds
View Data
Dataset View
Sort the observations by values in descending order in two variables
proc sql; create table sorted as select * from class order by age desc, height desc; quit;
Copy Code
View Log
SAS Log
proc sql; create table sorted as select * from class order by age desc, height desc; NOTE: Table WORK.SORTED created, with 19 rows and 5 columns. quit; NOTE: PROCEDURE SQL used (Total process time): real time 0.01 seconds cpu time 0.00 seconds
View Data
Dataset View
Count the number of records
Count the number of records with each unique value in sex variable
proc sql; create table counts01 as select sex,count(*) as count from class group by sex; quit;
Copy Code
View Log
SAS Log
proc sql; create table counts01 as select sex,count(*) as count from class group by sex; NOTE: Table WORK.COUNTS01 created, with 2 rows and 2 columns. quit; NOTE: PROCEDURE SQL used (Total process time): real time 0.01 seconds cpu time 0.01 seconds
View Data
Dataset View
Report the number of records within each sex,age combination
proc sql; create table counts01 as select sex,age,count(*) as count from class group by sex,age; quit;
Copy Code
View Log
SAS Log
proc sql; create table counts01 as select sex,age,count(*) as count from class group by sex,age; NOTE: Table WORK.COUNTS01 created, with 11 rows and 3 columns. quit; NOTE: PROCEDURE SQL used (Total process time): real time 0.00 seconds cpu time 0.00 seconds
View Data
Dataset View
Fetch descriptive statistics for numeric variables
Get the mean, standard deviation, minimum, maximum values of height within males and females
proc sql; create table stats01 as select sex ,mean(height) as mean , std(height) as sd , min(height) as min , max(height) as max from class group by sex; quit;
Copy Code
View Log
SAS Log
proc sql; create table stats01 as select sex ,mean(height) as mean , std(height) as sd , min(height) as min , max(height) as max from class group by sex; NOTE: Table WORK.STATS01 created, with 2 rows and 5 columns. quit; NOTE: PROCEDURE SQL used (Total process time): real time 0.00 seconds cpu time 0.00 seconds
View Data
Dataset View
Create macro variables
Create two macro variables named n1 and n2 to store the number of females and males from class dataset
proc sql noprint; select count(*) into :n1-:n2 from class group by sex; quit; %put &=n1 &=n2;
Copy Code
View Log
SAS Log
proc sql noprint; select count(*) into :n1-:n2 from class group by sex; quit; NOTE: PROCEDURE SQL used (Total process time): real time 0.00 seconds cpu time 0.00 seconds %put &=n1 &=n2; N1=9 N2=10
Create two macro variables named n1 to n6 to store the number of records in each age value from class dataset
proc sql noprint; select count(*) into :n1-:n6 from class group by age; quit; %put &=n1 &=n2 &=n3 &=n4 &=n5 &=n6; proc sql; create table counts01 as select age,count(*) as count from class group by age; quit;
Copy Code
View Log
SAS Log
proc sql noprint; select count(*) into :n1-:n6 from class group by age; quit; NOTE: PROCEDURE SQL used (Total process time): real time 0.00 seconds cpu time 0.00 seconds %put &=n1 &=n2 &=n3 &=n4 &=n5 &=n6; N1=2 N2=5 N3=3 N4=4 N5=4 N6=1 proc sql; create table counts01 as select age,count(*) as count from class group by age; NOTE: Table WORK.COUNTS01 created, with 6 rows and 2 columns. quit; NOTE: PROCEDURE SQL used (Total process time): real time 0.00 seconds cpu time 0.00 seconds
View Data
Dataset View
Create two macro variables named fcount and mcount to store the number of females and males from class dataset
proc sql noprint; select count(*) into :mcount from class where sex="M"; select count(*) into :fcount from class where sex="F"; quit; %put &=mcount &=fcount;
Copy Code
View Log
SAS Log
proc sql noprint; select count(*) into :mcount from class where sex="M"; select count(*) into :fcount from class where sex="F"; quit; NOTE: PROCEDURE SQL used (Total process time): real time 0.01 seconds cpu time 0.00 seconds %put &=mcount &=fcount; MCOUNT= 10 FCOUNT= 9