Friday, July 11, 2025

How to Create Data Groups Using PROC FORMAT in SAS: Real-World Examples

Problem/Requirement -

Lets Suppose, We have Data in Numbers which could be range from 100 to 10000
now we want to create Groups of Each 100 Values, like below - 

Groups Example in SAS



Lets See with the Example | The Power of the FORMAT Procedure - 


/*This code will Generate some Numbers between 100 and 10000 to Get the Practice Dataset */

Data A;
Num=100;
Do while(Num<10000);
output;
Num=Num+54;
end;
Run;
Proc Print Data=A;
Run;

Creating Groups in SAS


Now we want to Group it in as per given in Requirement 

/* This Code will Genrate FMTName (Format Name), Start(Starting Number of Group), End(Ending Number of Group), Label(Group name for Numbers between Start and End Values)
*/
Data B;
Retain FMTName "Group";
Start=100;
i=1;
Do until(End>=10000);
End=Start+99;
Label=compbl("Group"||i);
Output;
Start=End+1;
i=i+1;
end;
drop i;
run;

Proc Print Data=B;
Run;

Grouping Data in SAS

Now if we need to create a User Defined Format -

/* This wil create a format for Above created B Dataset with Format Name "Group", PROC FORMAT with the CNTLIN= option is to build the formats from the imported control data set */

Proc Format CNTLIN=B;
Run; 

/*Lets see how it will print A Dataset if we apply Newly create Group format to Variable Num */

Proc Print Data=A;
Format Num Group.;
run; 

USE of Proc Format in SAS
/* If we want Actual Number and the Group name both the things then we can do it like this */

Data C;
Set A;
GroupName=Put(Num,Group.);
run;
/*Here we created a new Variable Group name and Copy value of Num with Group format, For this we used PUT Function */
Proc Print Data=C;
Run;
USE of Proc Format in SAS


I Hope you like this Article, please comment if you have any doubt or question.
Thank you

Labels: , , , , , , , , , , ,

Tuesday, January 5, 2021

Understanding NOOBS, OBS=, N, and N in SAS Programming

When working with datasets in SAS, it's important to control data processing and output efficiently. SAS provides several options and automatic variables that help manage how data is read, processed, and displayed. In this post, we’ll explore four commonly used features:

  • NOOBS option in PROC PRINT
  • OBS= system option
  • _N_ automatic variable
  • N option in PROC MEANS or PROC FREQ

Let’s understand each with examples.

Use of NOOBS  

It is used for Suppress column which tells the row/observation number in Proc print.

Example - See both outputs with NOOBS and without NOOBS

Proc Print Data=SASHELP.CARS;
WHERE MAKE='Audi';
RUN;

Proc Print Data=SASHELP.CARS NOOBS;
WHERE MAKE='Audi';
RUN;

Use of Noobs

OBS= System Option

The OBS= option limits the number of observations that SAS reads from a dataset.

Syntax:
options obs=5;

Example:
options obs=5;
proc print data=sashelp.class;
run;

 Use of OBS  and N options - 

OBS option give the Actual row number from the original dataset, for example in below code output, row number starts from 8 because First row of Audi car in source dataset is available at 8th position


Proc PRINT data=SASHELP.cars N OBS;
where Make='Audi';
run;

Use of OBS and N - Datahark

N Option gives the Total count of observation in dataset.

Use of _N_ 

As I already explained in  my previous post for PDV - there are two automatic variables are being created in datastep these are - _N_ and _ERROR_

_N_ counts the Number of times Datastep begin to execute.

For Example - if we want to print the top 10 rows of any dataset - 

data test;
set SASHELP.CARS;
N=_N_;
if _n_<=10;
run;

Use of _N_ = Datahark

Summary Table

FeatureUsagePurpose
NOOBSIn PROC PRINTHide observation numbers in output
OBS=Global system optionLimit records read from datasets
_N_In DATA stepsCount iteration, control row logic
NIn summary proceduresDisplay count of non-missing values

Final Thoughts

These options and variables are small but powerful tools in a SAS programmer’s toolkit. Whether you're generating cleaner reports with NOOBS, testing code with OBS=, controlling logic with _N_, or summarizing data using N, understanding their usage can significantly enhance your efficiency in SAS.

Labels: , , , , , , , ,

Monday, March 30, 2020

闈 Creating Datasets in SAS: A Beginner’s Guide

Creating datasets(Tables) is the foundational step in SAS programming journey. Whether you are working on analytics, reporting, or data visualization, a strong understanding of how to create and manage datasets in SAS is crucial. This blog post will walk you through various methods to create datasets in SAS, along with examples and best practices.


🔍 What is a SAS Dataset?

A SAS dataset is a table consisting of rows (observations) and columns (variables). It’s the standard format in which SAS stores and processes data.

Each SAS dataset contains:

  • Descriptor portion: Metadata (e.g., variable names, types, labels)
  • Data portion: Actual data values stored in rows and columns


📌 Methods to Create Datasets in SAS

There are multiple ways to create datasets in SAS. Let’s explore the most common methods:


1️⃣ Using the DATA Step

The DATA step is the most common and powerful method to create datasets manually.

🔹 Syntax:

DATA dataset_name;
INPUT var1 $ var2 var3; DATALINES; data_line1 data_line2 ; RUN;

✅ Example:

DATA students;
INPUT Name $ Age Marks; DATALINES; John 16 85 Sara 17 90 Ali 15 88 ; RUN;

This code creates a dataset named students with three variables: Name, Age, and Marks.


2️⃣ Using the SET Statement

The SET statement is used to create a new dataset from an existing one.

✅ Example:

DATA top_students;
SET students; IF Marks > 85; RUN;

This code filters students with marks greater than 85 and stores them in a new dataset called top_students.


3️⃣ Importing External Files (CSV, Excel)

SAS can import data from external files using procedures like PROC IMPORT.

✅ Import CSV Example:

PROC IMPORT DATAFILE="C:\data\students.csv"
OUT=students DBMS=CSV REPLACE; GETNAMES=YES; RUN;

✅ Import Excel Example:

PROC IMPORT DATAFILE="C:\data\students.xlsx"
OUT=students DBMS=XLSX REPLACE; SHEET="Sheet1"; GETNAMES=YES; RUN;

4️⃣ Using INFILE and INPUT for Raw Data

For reading raw data from external text files:

✅ Example:

DATA employees;
INFILE "C:\data\employees.txt"; INPUT ID Name $ Salary; RUN;

5️⃣ Creating Temporary vs. Permanent Datasets

  • Temporary Dataset: Stored in WORK library, deleted after session ends
  • Permanent Dataset: Stored in a defined library and persists beyond sessions

✅ Temporary Dataset:

DATA work.sales;
INPUT Product $ Quantity; DATALINES; Laptop 5 Mouse 15 ; RUN;

✅ Permanent Dataset:

LIBNAME mydata 'C:\sasfiles';
DATA mydata.sales; INPUT Product $ Quantity; DATALINES; Laptop 5 Mouse 15 ; RUN;

🧠 Best Practices for Dataset Creation in SAS

  • Use meaningful and consistent variable names
  • Label datasets and variables for clarity
  • Validate data using PROC PRINT, PROC CONTENTS, and PROC MEANS
  • Use formats to enhance readability (e.g., date, currency)


🧪 Verify Your Dataset

✅ View Contents:

PROC CONTENTS DATA=students;
RUN;

✅ Print Records:

PROC PRINT DATA=students;
RUN;

🔚 Conclusion

Mastering dataset creation in SAS is essential for every SAS programmer. Whether you're reading external files, filtering data, or creating datasets manually, the techniques discussed here will help you manage your data efficiently. Once your datasets are ready, you can proceed with analytics, visualizations, or reporting using other powerful SAS procedures.

Labels: , , , , ,