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 have input data that will be numeric and/or character and will be anywhere from 1 to 8 characters in length. For example;

OPTION NOCENTER;         
DATA rawdata;            
INPUT data $ 1-8;        
DATALINES;               
CCFAIL                   
JCLERROR                 
166                      
1                        
2                        
U500                     
2947                     
SE37                     
;                        
PROC PRINT DATA = rawdata;

I need the final output to be 8 characters in width, aligned right and leading zeros on any value that begin with a number extending it to a length of 8 characters. For example

JCCFAIL

JCLERROR

00000166    

00000001      

00000002      

       U500   

00002947

       SE37

 

Any thoughts on how to accomplish this would be greatly appreciated.

1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

proc sql;

  select

    case (anyalpha(data)>0)

    case (notdigit(trim(data))>0)

      when (1) then right(data)

      else translate(right(data),'0',' ')

    end

    as data

  from rawdata;

quit;

 

additional edit: the TRANSLATE function translates characters in the first argument.  To me, it is counterintuitive that the 3rd arg is translated to the 2nd, but that's how it is.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

View solution in original post

4 REPLIES 4
mkeintz
PROC Star

You have the character values JCLERROR and CCFAIL left-justified in the desired output, but U500 and SE37 are right-justified.  What criterion are you using to make that distinction?

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
a079011
Obsidian | Level 7

Looks like copy/paste got me, desired out put should all be right justified.

data   
       
       CCFAIL
 JCLERROR
    00000166
    00000001
    00000002
           U500
    00002947
           SE37

mkeintz
PROC Star

proc sql;

  select

    case (anyalpha(data)>0)

    case (notdigit(trim(data))>0)

      when (1) then right(data)

      else translate(right(data),'0',' ')

    end

    as data

  from rawdata;

quit;

 

additional edit: the TRANSLATE function translates characters in the first argument.  To me, it is counterintuitive that the 3rd arg is translated to the 2nd, but that's how it is.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

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
  • 4 replies
  • 3224 views
  • 2 likes
  • 2 in conversation