BookmarkSubscribeRSS Feed
thanikondharish
Fluorite | Level 6

PROC SQL ;
CREATE TABLE AT AS
SELECT 'LABEL'||' '||STRIP(F1)||' '||"='"||STRIP(F3)||"'" AS NEW,
CASE WHEN UPCASE(F4)='STRING' THEN
STRIP(F1)||' '||"='"||STRIP(F3)||"'"||' LENGTH=$'||STRIP(F5)||'.' END
AS NEW1,
case WHEN CALCULATED NEW1=' ' THEN NEW END AS NEW2
FROM S1 ;
QUIT;

 

how to keep program lines in a order like see below ANY SHORT CUT KEY OR PROCEUDURE

PROC SQL ;
           CREATE TABLE AT AS
                  SELECT 'LABEL'||' '||STRIP(F1)||' '||"='"||STRIP(F3)||"'" AS NEW,
                 CASE WHEN UPCASE(F4)='STRING' THEN
                STRIP(F1)||' '||"='"||STRIP(F3)||"'"||' LENGTH=$'||STRIP(F5)||'.' END AS NEW1,
                 case WHEN CALCULATED NEW1=' ' THEN NEW END AS NEW2
              FROM S1 ;
QUIT;

6 REPLIES 6
thanikondharish
Fluorite | Level 6
No it didn't work
when i presss ctrl+i it shows sas system options like see photo
PeterClemmensen
Tourmaline | Level 20

See @ChrisHemedingers blog post Hope For Ugly Programs

 

You can use SAS Enterprise Guide and use CTRL+i to indent your code properly. You can go to Program --> Editor Options and select the Indenter tap to customize how you want the indentation done. 

 

Capture.PNG

RW9
Diamond | Level 26 RW9
Diamond | Level 26

You should also be able to highlight rows and then press the tab key to see rows indented.  Note, good idea to set the tab key up to insert spaces rather than tabs as tabs render differently on different editors.  

Also, some other tips - avoid using all uppercase, its shouting.  Use a code window - its the {i} above post area for code, this retains formatting and highlights code for easy viewing as;

data want;
  set have;
run;

Use cat() functions rather than lists separated by || as its easy to read (its more important that code be documented and readable at the end of the day).  Don't split code over lines unlessyou really have to.

So to change your code.

proc sql;
  create table at as
  select cat('label ',strip(f1),' ','=',quote(strip(f3))) as new,
         case when upcase(f4)='STRING' then 
cat(strip(f1),' ',"=",quote(strip(f3)),' length=$',strip(f5),'.') end as new1, case when calculated new1=' ' then new end as new2 from s1; quit;;
ballardw
Super User

FWIW you might be able to make your code easier to read and follow by using the CAT functions

 

Instead of

'LABEL'||' '||STRIP(F1)||' '||"='"||STRIP(F3)||"'" AS NEW,

try

 

catx(' ','Label', strip(f1),'=', quote(strip(f3)) ) as new

And if the purpose of strip(f1) is to remove possible trailing blanks then the STRIP() is not needed as CATX removes trailing blanks when concatenating values. The CATX function first parameter is a delimiter to separate the other character elements, so the above inserts a space between each bit which is a whole lot nicer than a lot of ||' '|| constructs when it comes to reading the code. The delimiter can be any string literal value.

 The QUOTE function will also place quotes around text in a way that is somewhat easier to understand than  "'"||STRIP(F3)||"'" though it does want the STRIP to avoid having trailing blanks inside the applied quotes.

 

The more elements you are placing in a string the easier using CATX becomes compared to multiple || operators.

 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 6 replies
  • 1413 views
  • 0 likes
  • 5 in conversation