Published on :
Macro CREATION_INTERNE

Extracting the first element of a string with %SCAN

This code is also available in: Deutsch Español Français
The main objective of this code is to illustrate how to manipulate character strings stored in macro variables. It begins by defining a macro variable named `varlabel` containing several comma-separated values. Then, the %SCAN macro function is used to isolate the first 'word' or 'token' from this string using the comma as a delimiter. The result is stored in a new macro variable, `bbb`. Finally, the value of `bbb` is displayed in the SAS© log via the %PUT statement. The 'quit;' command is present but without a previous active SAS© procedure, it has no notable functional effect in this context.
Data Analysis

Type : CREATION_INTERNE


The manipulated data are character strings defined directly in the script via macro variables. There is no reading of external or internal SAS datasets (like SASHELP) nor creation of traditional SAS datasets.

1 Code Block
Macro
Explanation :
This block initializes the macro variable `varlabel` with a list of vaccines. It then uses the `%SCAN` macro function to extract the first value ('Flu vaccine') from `varlabel`, using the comma as a separator, and assigns it to the macro variable `bbb`. The `quit;` statement terminates any potentially open SAS procedure, although none is active here. Finally, `%PUT` writes the value of `bbb` to the SAS log, thereby verifying the correct extraction.
Copied!
1%let varlabel= Flu vaccine,Tetanus-diptheria,Hepatitis B vaccine,Hepatitis A vaccine,PPD,Pneumovax;
2 
3%let bbb= %scan("&varlabel",1,',');
4QUIT;
5%put the 1st is &bbb;
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.