SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
HeatherNewton
Quartz | Level 8
dual = compress(substr(acc,1,8) || put (( input(substr(acc,9,1),1.),5.) ||
substr(acc,10,7) || put (( input(substr(acc,17,1),1.)+1),5.) || substr (acc,18,2)," ");

I have trouble understand the above, could you enlighten me?

especially the put, input part, is it bad practice to write this kind of code? why so complicated, there is no single function to do put, input together? 

very scary code the big paragraph above...

2 REPLIES 2
Tom
Super User Tom
Super User

What does ACC contain?
What do you want DUAL to contain?

 

You seem to have too many left parenthesis. In particular why are their two left parenthesis in the PUT() function calls?

 

Even if we fixt that doesn't make much sense

put( input(substr(acc,9,1),1.) , 5. )

So you take a single character from ACC, convert it to a number between 0 and 9 then write it back as a character string with four spaces and the original single digit again and then later you remove the four spaces that were inserted. So why not just use:

substr(acc,9,1)

You can reduce this a lot by using the CATS() function instead of || operator. Then there is might be no need for the COMPRESS() function.

23820  data test;
23821   acc='................7...';
23822   dual=cats(substr(acc,1,16),input(substr(acc,17,1),1.)+1,substr(acc,18,2));
23823   put acc / dual ;
23824  run;

................7...
................8..

Or perhaps even simplier

dual=acc;
substr(dual,17,1) = put(input(substr(dual,17,1),1.)+1,1.);
ballardw
Super User

Without seeing actual values or understanding why it was written can't say if good or bad though there is very likely a lot of marginal at best code in this mess.

 

Example this bit reads one character out of the variable, converts it to a number, which would be a sing digit and then puts it as 5 characters which be default means 4 leading spaces

put (( input(substr(acc,9,1),1.),5.)

This is marginally better as the input bit has something added to it so justifies the numeric conversion and could result in either a 1 or 2 digit number. Again it is put using 5 characters though

put (( input(substr(acc,17,1),1.)+1),5.)

and the compress removes the blanks. So the first bit I discuss above likely should have stopped with the

substr(acc,9,1)

But as I mentioned, without knowing 1) actual values, 2) what the result is supposed to be and 3) why the result should look that way, I can't say (That's a large economy sized hint). Was there any comment in the code or program description about what was being fixed and why?

 

Not the ugliest thing I've written or anywhere near the longest, but when there long strings that need massaging to specific appearance such things happen.

 

 

 

Sometimes such a combination of put and input is used because the original data was read poorly and fixes are needed. One such is when a "date" that looks like 20210517 was read as simple number but the desire is to have an actual SAS date value. The original source data should have been read with a yymmdd informat. Instead of writing a bunch of code to parse out bits it is simpler to Put the number into a text value that an Input function can use the proper input.

 

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

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