BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
SasStatistics
Pyrite | Level 9

Assume I have a data set containing a column with different names of some random items. 


First row could be: This_Is_My_First_item

Second row could be: A

Third row could be: ABC

.

.

.


Now I want to crate a report with all items whose name has length of 1. This is intuitively very simple to do (atleast in other programming languages) but it was not trivial to do for me in SAS. 

The solution I used at the end was: 

 

proc print data=some_random_data_set;
	 where name like "_";  
run;

Any other solutions to this? 

Intuitevely (I am currently new to SAS) I wanted to use something like the code below which is some pseudo-code:

 

proc print data=some_random_data_set;
	 if length(name) = 1 then print;  
run;

 

So the question is: 

1. Any other (Better?) solutions to this problem?

2. I actually spent quite a lot of time doing this and it is a bit troublesome for me that these kind of simple things are not so straightforward to do nor to find a solution to. 

 

Thanks.

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

@SasStatistics wrote:

Assume I have a data set containing a column with different names of some random items. 


First row could be: This_Is_My_First_item

Second row could be: A

Third row could be: ABC

 


Is the variable of interest NAME? Because you don't really say ...

 

Here's an example using data set SASHELP.CLASS

 

proc print data=sashelp.class;
    where length(name)=4;
run;

If the variable is named something else, then you would make the obvious change to the above code.

--
Paige Miller

View solution in original post

4 REPLIES 4
CarmineVerrell
SAS Employee

You would probably need to use a data step to flag those records that you want to include that only have a length of one.

 

Data newfile;

     set yourdata;

     if length(column)=1 then flag=1;

run;

 

proc print data=newfile;

where flag=1;

run;

PaigeMiller
Diamond | Level 26

@SasStatistics wrote:

Assume I have a data set containing a column with different names of some random items. 


First row could be: This_Is_My_First_item

Second row could be: A

Third row could be: ABC

 


Is the variable of interest NAME? Because you don't really say ...

 

Here's an example using data set SASHELP.CLASS

 

proc print data=sashelp.class;
    where length(name)=4;
run;

If the variable is named something else, then you would make the obvious change to the above code.

--
Paige Miller
SasStatistics
Pyrite | Level 9
The variable of interest is NAME.

This solution is what I intuitively wanted to do. Thanks.
Tom
Super User Tom
Super User

You can call functions in WHERE statements.

 

Note that LENGTH() will return 1 when the value is empty (all blanks).  If you want to also exclude the empty strings you can use LENGTHN() function instead and it will instead return 0 for empty strings.

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1048 views
  • 6 likes
  • 4 in conversation