Read Data Sas Different Columns Using "@"
SAS data sets consist of a descriptor portion and a data portion that contains the data values. The descriptor portion of a SAS data set holds the detailed data about the data set. This information includes:
- Dataset proper noun and its ember blazon
- Creation fourth dimension of the dataset
- Thenumber of observations
- The number of variables
- The engine type
Nearly of the times we need to count the numbers of observations in a SAS dataset and pass it to a macro variable. Reading the descriptor portion is i of the most efficient and quickest ways of determining the number of observations in a SAS data set.
In this postal service, we volition see various methods to count the number of rows (records) in a SAS dataset.
ane. Using PROC SQL
proc sql; select count( *) into :cnt from sashelp.class; quit; %put &cnt.;
proc sql; select count(*) into :cnt from sashelp.class; quit; %put &cnt.;
Using the PROC SQL method is not considered to be an efficient manner equally information technology does not employ metadata data of the SAS dataset. Instead, it reads through each tape (row) of your SAS dataset which requires processing power. However, it is elementary to understand and develop and tin be used for smaller datasets.
two. Using End= Argument
data _null_; set up sashelp.class end=eof; count+1; if eof then call symput( "nobs", count); run; %put &nobs;
data _null_; set sashelp.course end=eof; count+1; if eof so phone call symput("nobs", count); run; %put &nobs;
This method is also not efficient. It reads the entire information set and increments a counter to pick up the last value of the dataset. The Finish option returns true or 1 if the observation is the last observation in the dataset.
three. Using the Data Step
data _null_; set sashelp.course nobs=obs; call symputx( 'nobs', obs); run; %put &nobs.;
data _null_; set sashelp.class nobs=obs; call symputx('nobs', obs); run; %put &nobs.;
NOBS is a SAS automatic variable that contains the number of rows in a dataset and NOBS = obs holds the count of records in the variable obs.
Call SYMPUTX is a DATA Step call routine that assigns a value produced in a Data footstep to a macro variable.
4. Using IF 0 and STOP statement
data _NULL_; if 0 then prepare sashelp.class nobs=n; call symputx( 'totobs', n ); finish; run; %put no. of observations = &totobs;
information _NULL_; if 0 and so prepare sashelp.form nobs=n; call symputx('totobs', n); finish; run; %put no. of observations = &totobs;
During the compilation phase, the information step reads in variables from the data set in the Gear up Statement. During execution, SAS reads in the observations from the input data set in sequential order. By using if 0 then ready, we can bypass the execution part of the Set Argument which is conditional logic that ever fails .
The 'if 0' statement is not processed at all considering the IF statement does not hold TRUE. The whole IF-Then statement is used to pull the header information of the data set and after laissez passer information technology to the compiler to adjust it to the PDV.
If 0 Then Gear up can also be coded equally If (1=2) Then Set
The Cease argument is used to stop an countless loop.
Read – Exploring the Fix Argument in SAS
v. Proc SQL Lexicon Method
proc sql noprint; select nobs into :totobs separated by ' ' from dictionary.tables where libname='SASHELP' and memname='CARS'; quit; %put total records = &totobs.;
proc sql noprint; select nobs into :totobs separated by ' ' from lexicon.tables where libname='SASHELP' and memname='CARS'; quit; %put full records = &totobs.;
The metadata information of a dataset tin can be accessed with PROC SQL Dictionary.Tables.It is an efficient method as it does not look into each value of a dataset to decide the count.
The LIBNAME= refers to the name of the library in which data is stored. TheMEMNAME= refers to the SAS tabular array (dataset). The separated by ' ' is used in this case to left-align the numeric value.
half dozen. Using the Library tabular array – SASHELP.VTABLE
information _null_; set sashelp.vtable; where libname="SASHELP" and memname="CLASS"; Num_obs=Nobs-Delobs; put "Nobs - Delobs: num_obs = " num_obs; call symputx( 'obs_cnt',num_obs); run; %put &obs_cnt;
data _null_; prepare sashelp.vtable; where libname="SASHELP" and memname="CLASS"; Num_obs=Nobs-Delobs; put "Nobs - Delobs: num_obs = " num_obs; call symputx('obs_cnt',num_obs); run; %put &obs_cnt;
Equally we know that SAS library tables contain metadata relating to your data sets, and we tin get this information from the SASHELP.VTABLE past specifying the LIBNAME and the proper name of the data set.
The two variables you need from the table are Nobs which contains the total number of observations including ones marked for deletion and Delobs contains but the number of observations marked for deletion.
This method of determining the number of observations in a SAS data gear up has an advantage over the previous methods described so far. That is if you (or other people) are modifying a information prepare, you lot need to know the total number of observations in a data gear up as well equally the number of observations that have been marked for deletion (but are still counted when you apply the NOBS= SET option).
7. Using PROC SQL automatic variable – SQLOBS
proc sql noprint; select * from sashelp.class; run; %put &sqlobs.;
proc sql noprint; select * from sashelp.course; run; %put &sqlobs.;
Proc SQL automatically creates SALOBS macro variable, when it runs a SQL Footstep. SQLOBS macro variable will have the number of observations count of the terminal proc SQL statement executed.
8. Using Information Access Functions
%macro totobs(mydata); %let mydataID=%sysfunc ( Open up ( &mydata.,IN ) ); %let NOBS=%sysfunc ( ATTRN ( &mydataID,NOBS) ); %let RC=%sysfunc ( Shut ( &mydataID ) ); &NOBS %mend; %put %totobs(sashelp.cars);
%macro totobs(mydata); %allow mydataID=%sysfunc(OPEN(&mydata.,IN)); %let NOBS=%sysfunc(ATTRN(&mydataID,NOBS)); %let RC=%sysfunc(CLOSE(&mydataID)); &NOBS %mend; %put %totobs(sashelp.cars);
References
How Many Observations Are In My Data Set up?
Source: https://www.9to5sas.com/count-the-number-of-observations-in-a-sas-dataset/
Enregistrer un commentaire for "Read Data Sas Different Columns Using "@""