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

hello,  i'm trying to use a subsetting to filter a dataset.   I imported a .csv file into SAS.  import worked as expected, but when I try to filter the dataset using the following: 

 

length Industry_classification_code $10;

Industry_classification_code = 'Industry classification code'n;
if length(trim(Industry_classification_code)) = 6;

 

My dataset is get created without any rows.   I checked my dataset before the if statment.  Even did a proc freq, the values are there.   'Industry classification code'n is a numeric variable values look like:

92000

92011

529983

674552

I'm trying to keep rows where there are six digits in the variable,  like the last two rows above.  Something is wrong, not sure what.  Any assistance would be greatly appreciated.  

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

You say the variable is numeric. That means that the length is very likely to be 8 as that is the default length for numeric values. If you want to convert the variable to character so you get an expected length try

 

Length (put('Industry classification code'n , f10. -L)

The put creates a character value using the format F10, using you apparently desired length. The -L means to left justify the result. A numeric 92000 would then become "92000   ". The Length function will ignore trailing blanks.

 

You can see the result with this:

data junk;
   x= 92000;
   l = length(put(x,f10. -L));
run;

Personally I read CSV files so the variables are of the types I want using a data step. I suspect you allowed Proc Import to create a SAS data set and because of options set you now have 1) name literals like that ugly thing that are somewhat awkward to code with, and 2) variables of the incorrect type.

View solution in original post

3 REPLIES 3
ballardw
Super User

You say the variable is numeric. That means that the length is very likely to be 8 as that is the default length for numeric values. If you want to convert the variable to character so you get an expected length try

 

Length (put('Industry classification code'n , f10. -L)

The put creates a character value using the format F10, using you apparently desired length. The -L means to left justify the result. A numeric 92000 would then become "92000   ". The Length function will ignore trailing blanks.

 

You can see the result with this:

data junk;
   x= 92000;
   l = length(put(x,f10. -L));
run;

Personally I read CSV files so the variables are of the types I want using a data step. I suspect you allowed Proc Import to create a SAS data set and because of options set you now have 1) name literals like that ugly thing that are somewhat awkward to code with, and 2) variables of the incorrect type.

covershaker
Calcite | Level 5
Thanks for the quick response. That did the trick.
Kurt_Bremser
Super User

Maxim 2: Read the Log. You will see a NOTE about conversion of numeric to character.

LENGTH is a character function, so the numeric value is converted with the BEST12. format, right-aligned, which causes your LENGTH call to return an unexpected result.

Try

if length(strip(put('Industry classification code'n,best.))) = 6;

or

if 100000 le 'Industry classification code'n le 999999;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 3 replies
  • 1170 views
  • 0 likes
  • 3 in conversation