SAS | How to read character of varying length using COLON Modifier

We generally face this situation when we have company names or both first and last names of a person in our data set.

We can use a colon modifier “:” to tell SAS to read variable “Name” until there is a space or other delimiter. The $30. refers to the variable as a character variable having max length 30.

filter_none

edit
close

play_arrow

link
brightness_4
code

data ex1;
input ID Name :$30. Score;
cards;
1 ShubhamMaurya  25
2 SaurabhPandey  32
3 NikitaChaudhary  30
;
proc print;
run;

chevron_right


Output:



The colon modifier is also helpful to read numeric data that contains special characters such as comma(, ).

For example,

1, 000

For example, if you want to read a variable which holds numeric values with the comma in thousands place (or thousand separators) then refer the code below.

filter_none

edit
close

play_arrow

link
brightness_4
code

data ex2;
input ID Name:$30. Score fee:$10.;
cards;
1 ShubhamMaurya  25 1, 000
2 SaurabhPandey  32 2, 000
3 NikitaChaudhary  30 3, 000
;
proc print;
run;

chevron_right


Output:

In the above program, we have declared “fee” variable with colon modifier to load data and used $ sign to read this variable. It will be stored as a character variable. If you would not use $ sign, it will return missing values. Refer the program below how to store it as a numeric variable.

filter_none

edit
close

play_arrow

link
brightness_4
code

data ex2;
input ID Name:$30. Score fee comma5. ;
cards;
1 ShubhamMaurya  25 1, 000
2 SaurabhPandey  32 2, 000
3 NikitaChaudhary  30 3, 000
;
proc print;
run;

chevron_right


Output:

Note: comma5 format removes comma and store it as a numeric variable. 5 refers to the width of the input field. To read a bigger number such as 5, 000, 000, you can use comma10.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.