SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
a079011
Obsidian | Level 7

I am trying to use compress and I am not getting the results I expected. Here is my code;

OPTION NOCENTER ERRORS=1;      
DATA espin;                    
 FORMAT n_job  $CHAR8.;                                   
 txt = 'ITCSUN.PLF(AITCC02)';                           
 e_ptr=INDEX(txt,'(');         
 IF e_ptr>0;                   
   n_job=SUBSTR(txt,1,e_ptr-1);
   n_job=COMPRESS(n_job,'.');  

 

 

after i run this my n_job is "ITCSUNP".

Obs    n_job               txt                                 e_ptr                                               
 1     ITCSUNP     ITCSUN.PLF(AITCC02)      11

 

what I expected it to be is "ITCSUN" (without the P). Cannot figure out why compress is keeping the only the "p" and not cutting off everything after the '.'.

 

Any thoughts, help or suggestions would be greatly appreciated.

1 ACCEPTED SOLUTION

Accepted Solutions
ScottBass
Rhodochrosite | Level 12

txt = 'ITCSUN.PLF(AITCC02)';  

 

e_ptr=INDEX(txt,'('); finds the column position of the first '('

 

n_job=SUBSTR(txt,1,e_ptr-1); sets n_job to the string starting from 1 to the column position of '(' - 1.

 

So n_job is now 'ITCSUN.PLF'

 

n_job=COMPRESS(n_job,'.');  removes '.' from n_job, so n_job is now 'ITCSUNPLF'

 

And you're formatting n_job as $CHAR8., so n_job *appears* as ITCSUNPL

 

If you want to retain only the text preceding the '.', consider using the SCAN function instead.  If what you want is 'ITCSUN', then all of that can be simplified as n_job=scan(txt,1,'.');

 

Consider removing or commenting out your format statement until you get the data transformation code debugged.  In fact I doubt you need the format statement at all.


Please post your question as a self-contained data step in the form of "have" (source) and "want" (desired results).
I won't contribute to your post if I can't cut-and-paste your syntactically correct code into SAS.

View solution in original post

1 REPLY 1
ScottBass
Rhodochrosite | Level 12

txt = 'ITCSUN.PLF(AITCC02)';  

 

e_ptr=INDEX(txt,'('); finds the column position of the first '('

 

n_job=SUBSTR(txt,1,e_ptr-1); sets n_job to the string starting from 1 to the column position of '(' - 1.

 

So n_job is now 'ITCSUN.PLF'

 

n_job=COMPRESS(n_job,'.');  removes '.' from n_job, so n_job is now 'ITCSUNPLF'

 

And you're formatting n_job as $CHAR8., so n_job *appears* as ITCSUNPL

 

If you want to retain only the text preceding the '.', consider using the SCAN function instead.  If what you want is 'ITCSUN', then all of that can be simplified as n_job=scan(txt,1,'.');

 

Consider removing or commenting out your format statement until you get the data transformation code debugged.  In fact I doubt you need the format statement at all.


Please post your question as a self-contained data step in the form of "have" (source) and "want" (desired results).
I won't contribute to your post if I can't cut-and-paste your syntactically correct code into SAS.

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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
  • 1 reply
  • 1422 views
  • 2 likes
  • 2 in conversation