Logical variables in SAS

Blog Author


Published Date

21st September, 2018




  • Share this blog :

Logical variables:

We are working based on 2 variables

  1. First.Variable
  2. Last.variable
  • If First. variable is 1, then we will get first occurrence.
  • If First. variable is 0, then we will get remaining occurrence except first occurrence.
  • If Last. variable is 1, then we will get last occurrence.
  • If last. variable is 0, then we will get except last occurrence include first occurrence.

/* To report first occurrence information */ Ex: Data medi; Input Gid $drug$ sdate edate ; Informat   sdate edate  date9. Format sdate edate date 9; Cards; G100   col5mg    12jan2006   16jan2006 G101  col5mg    13jan2006   17jan2006 G102  col5mg    13jan2006   17jan2006 G101   col10mg    21jan2006   25jan2006 G102   col5mg      23jan2006   27jan2006 G100   col10mg    23jan2006   29jan2006 G100   col15mg    01feb2006   05feb2006 G101   col10mg    03feb2006   07feb2006 G102   col15mg    03feb2006   06feb2006 G103   col10mg    03feb2006   04feb2006 ; Proc sort data = medi; By Gid; Run; Proc print data =medi; Run; Data first; Set medi; By Gid; If first .gid =1; Run; Proc print data =first; Run;

/* To report remaining occurrence except first to report each and every patient except first visit information*/

Ex; Data remain1; Set medi; By gid; If first.gid=0; Run; Proc print data=remain1; Run;

/* To report last occurrence –subject of each patients last visiting information*/

Ex: Data last; Set medi; By gid; If last.gid=1; Run; Proc print data=last; Run;

At TekSlate, we offer resources that help you in learning various IT courses. We avail both written material and demo 
video tutorials. To gain in-depth knowledge and be on par with  practical experience, then explore SAS Training
/* To report each and every patients visiting information except last visit information*/

Ex; Data remain2; Set medi; By gid; If last.gid=0; Run; Proc print data=remain2; Run;

/* To report first and last occurrence information*/

Ex; Data firstlast Set medi; By gid; If first.gid=1 or last.gid =1; Run; Proc print data=firstlast; Run;

/* To report which group visited only once*/

Ex; Data flast; Set medi; By gid; If first.gid=1 and last.gid=1; Run; Proc print data=flast; Run;

/* To report remaining occurrence except first and last*/

Ex; Data remain3; Set medi; By gid; If first.gid=0 and last.gid=0; Run; Proc print data=remain3; Run;  

Retain statement:

        It can be used to calculate running total or cumulative tools.

  • Using retain statement, we can reassign required variables current data value.
  • If we omit 0, it default stores dot(.).

Ex: Data market; Input month $sale; Cards; Jan 200 Feb 300 Mar . Apr 400 May 500 June 200 ; Data market 1; Set market; Retain total 0; Total = sum (total, sale); Run; Proc print data = market1; Run; To generate the output window with out using any procedure application(use with data set block) Ex; Data _null_; Set market; File print; Put month sale; Run;  

Null data set creation:

(with variable and with out observations) using with data set block /* To delete required observations from existed data set*/ Ex: Data market ; Set market; If sale < 400 then delete; Run;

  • If we run delete statement without any condition it delete overall data from existing data set.

Ex: Data market1 ; Set market; delete; Run; Ex: Data market1 ; Set market; If sale >=400 ; Run;

/* null data set creation */

Ex: Data demo; Length pid 4 age 3 Race $ 6; delete; Run;

  • -Null-, without data set creation.
  • Null data set, with variables and with out observations.

For indepth knowledge on SAS, click on below

About Author


Author Bio

TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills .

Related Blogs

Write For Us