Published on :
Data Manipulation CREATION_INTERNE

Substring Extraction

This code is also available in: Deutsch Español Français
Awaiting validation
The script begins by creating a temporary dataset named 'test1' with a single variable 'x' containing the string 'pradeep*'. Next, a second DATA step creates the 'test2' dataset from 'test1'. In this step, a new variable 'y' is generated by extracting the first two characters of the 'x' variable using the SUBSTR function. Finally, the PROC PRINT procedure is used to display the content of the 'test2' dataset, thus showing the result of the substring operation.
Data Analysis

Type : CREATION_INTERNE


The data used ('pradeep*') is defined directly in the SAS script via a DATALINES statement. There is no dependency on external datasets or the SASHELP library.

1 Code Block
DATA STEP Data
Explanation :
This DATA STEP block creates a temporary dataset named 'test1'. It defines a character variable 'x' and uses the DATALINES statement to populate the dataset with the value 'pradeep*'. This step is essential to provide the base data for subsequent transformations.
Copied!
1DATA test1;
2 INPUT x $;
3 DATALINES;
4pradeep*
5;
2 Code Block
DATA STEP Data
Explanation :
This second DATA STEP block creates a new dataset 'test2'. It reads observations from the 'test1' dataset (previously created) and applies the SUBSTR function to the 'x' variable. The SUBSTR(x,1,2) function extracts a substring from 'x' starting at the first position with a length of 2 characters, storing the result in the new variable 'y'.
Copied!
1DATA test2;
2 SET test1;
3 y=substr(x,1,2);
4 
3 Code Block
PROC PRINT
Explanation :
This block uses the PROC PRINT procedure to display the content of the 'test2' dataset. This allows visualizing the data after the substring operation and verifying the result of the 'y' variable creation. The 'run;' statement executes the procedure, and 'quit;' terminates active procedures and releases associated resources.
Copied!
1PROC PRINT DATA=test2;
2 
3RUN;
4QUIT;
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.