SAS Programs for beginners

Here you can find out SAS programs for beginners. Just copy these programs into your SAS software and then run. In this way you will better understand the program. Always remember to see the Log window so that you can see the errors in your programs. If you are good in error handling then you will become a good SAS programmer.

How to Sort a Dataset

Suppose you have two datasets and you want to arrange them in a proper order then you can use the code “PROC SORT” to sort  them in proper order. In the below listed example I have sorted the data by country name. That means the countries will be shown in alphabetically sorted order. And the whole dataset will be shown in the sorted order by country.

data new;
input name $ country $ id;
cards;
Rohan India   0001
John  China   0002
Sam   Brazil  0003
Arun  America 0004
;
run;
proc sort data=new;
by country;
run;

How to Merge two dataset

If we are having two different datasets and we want to join them in one dataset then we can use the “MERGE”. In the below listed example I have first sort the two datasets by country and then I have created a third dataset and merge the data one and data two in the third one. We can also use the datasets that are stored in our computer. Firstly we have to fetch them into the SAS environment and after that we can apply the MERGE on them in order to make one dataset.

data one;
input country$ population;
cards;
India    1110032
RSA      1111254
USA      11147855
Japan    25464
UK       114785
;
run;
proc print data=one;
run;
proc sort data=one;
by country;
run;
data two;
input country$ population;
cards;
RSA 114458
UAE 221587
UK 1147856
;
run;
proc print data=two;
run;
proc sort data=two;
by country;
run;
data three;
merge one two;
by country;
proc sort data=three;
by country;
run;
proc print data=three;
run;

Demo of PDV

PDV stands for “Program Data Vector”. It is very important in SAS you can check the execution of a sas program by using PDV. You can easily find out the errors in your program by using PDV.

TITLE "PDV Demo";
data demoPDV;
put "after compile beofre execution: "_all_;
x="cat";
y="dog";
x="mouse";
n=5;
k=n*2;
put "at the end of the execution: " _all_;
run;
proc print data=demoPDV;
run;

Input Statement

The INPUT statement creates a variable using the name that you assign to each field. Therefore, when you write an INPUT statement, you need to specify the variable names exactly as you want them to appear in the SAS data set.

List Input Method

Title "List Input Method";
Data demo;
input id name $ city $;
cards;
001 John  New York
002 Greg  Texas
003 Neo   London
004 Paul  Amsterdam
005 Alex  Paris
;
run;
proc print data=demo;
run;

NAMED INPUT METHOD

Title "Named Input Method";
Data demo;
length name $ 14;
input name= $ id= class= section= $ age=;
cards;
id=1 name= Greg Carry class= 8 section=A age= 12
id=2 name= Han class= 8 section=A age= 13
id=3 name= Helan class= 8 section=B age= 15
;
run;
proc print;
run;

COLUMN INPUT METHOD

Title "Column Input Method";
Data demo1;
Infile cards;
Input id 1-4 name $ 5-13 age 14-16 color $ 18-23 date mmddyy10.;
Cards;
100 Raj       35 White   11/12/1993
101 Rohan Roy 55 Black   9/2/1991
;
run;
proc print;
run;

FORMAT INPUT METHOD

Title "Format Input Method";
data demo3;
infile cards;
input +0 id 3. +1 name $ 31.;
cards;
100 Rahul
101 Priya
103 Ravi Shankan Ayyar Sri Bohale
;
run;
proc print data=demo3;
run;

ABSOLUTE INPUT METHOD

Title "Absolute Input Method";
data demo5;
infile cards;
input @1 id 3. @5 name $ 11.
@17 age 2. @20 race $ 5.;
cards;
100 kiran kumar     89 white
101 pawan           67 black
102 kranthi         89 white
;
run;
proc print data=demo5;
run;

Modifying List Input Method

Title "Modifying List Input Method";
data sales1;
infile cards;
input Emp_Id    : $4.
Name            & $15.
Region          : $5.
Customer        & $18.
Date            : mmddyy10.
Item            : $8.
Quantity        : 5.
UnitCost        : dollar9.;
Total_Sales = Quantity * UnitCost;
Next_Month_Sale = Total_Sales - 100;
New_Sales_Data = Next_Month_Sale * 100 + 520;
cards;
1843 George Smith North Barco Corporation 10/10/2006 144L  50    $8.99
1845 Arion Patler South Cost Cutter's     10/11/2006 122   100   $5.99
1547 Greg         West  Barco             11/07/2015 146L  120   $10.59
;
run;
proc print data=sales1;
run;

Line Pointer Controls

Title " Line Pointer Controls ";
data new;
infile cards;
input #1 Name $40.
#2 Street $40.
#3 @1 City $20.
@21 State $2.
@24 Zip $5.;
cards;
Herry Smith
2274 High fields 480
camp de ver    LA 78024
;
run;
proc print;
run;

data fresh ;
input Fname$ 1-8  Lname $ 10-15 /
Department $ 1-12 Jobcode $ 15-19 /
Salary comma10. ;
cards ;
goel     varun
marketing     sr01
$25,209.03
;
run;
proc print;
run;

Mixed Input Method

Title 'mixed input method';
Footnote 'End of Program';
data new;
infile cards;
input @1 id 3. +1 name $ 11. +1 class $ 11. +1 section $ 2.;
cards;
100  Rahul      12               B
101  Rohan Raj  10               A
102  John Smith Engineering      C
;
run;
proc sort data=new;
by section;
run;
proc print;
run;

How to Use KEEP in SAS

proc contents data=Nations;
run;
data nations1 (keep = obs country);
set Nations;
run;
data nations2;
set nations (keep = obs country pop83);
run;
data sasuser.nations3;
set work.nations;
keep obs;
run;

Select only one observation from a large dataset

If you are having a large dataset and you only want to select in observation from that dataset then you can use a “FIRSTOBS” and “OBS” select the observation that you want. You can check the below listed example just copy it and paste it in the SAS software. Then you will be easily understand the process.

data new;
infile cards firstobs=2 obs=2;
input +0 id 3. +1 name $ 14. +1 class 2. + 1 section $ 2.;
cards;
100 Raghu Raja Ram 12 B
101 Rohan          10 A
102 Mac             9 C
103 Smith Ferry     8 D
;
run;
proc print;
run;

Comments

comments

Sanaul

I am having more than 5+ years of experience in SAS Programming and Analysis. But I love to write that's why I am a freelancer writer. So that I can share my experience by working part time as a blogger. I used to travel and explore more and more. Please see my various articles about travel and tourism here

Leave a Reply

Your email address will not be published. Required fields are marked *

two × 1 =

Time limit is exhausted. Please reload CAPTCHA.