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.
@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_itemSecond 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.
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;
@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_itemSecond 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.
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.
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!
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.
Ready to level-up your skills? Choose your own adventure.