DATA Step, Macro, Functions and more

Doubt in Trim portion in the below code

Reply
New User
Posts: 1

Doubt in Trim portion in the below code

     ARRAY SCD(*)   SC001-SC500;

      DO IS = 1 TO 500;

          IF SUBSTR(SCD(IS),1,1) > "V" THEN I = 500;

          ELSE

          IF SUBSTR(SCD(IS),1,1) = '*' THEN

            DO;

             SUBSTR(TRIM,1,2) = SUBSTR(SCD(IS),2,2);

             TRIM1 = SUBSTR(SCD(IS),1,3);

            END;

          ELSE

          IF SUBSTR(SCD(IS),1,1) = '-' THEN

            DO;

             SUBSTR(TRIM,3,2) = SUBSTR(SCD(IS),2,2);

             TRIM2 = SUBSTR(SCD(IS),1,3);

            END;

Super User
Posts: 6,962

Re: Doubt in Trim portion in the below code

a) don't write program code in all capitals

b) what's your doubt?

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Contributor
Posts: 52

Re: Doubt in Trim portion in the below code

do you have doubt in below statement:

SUBSTR(TRIM,1,2) = SUBSTR(SCD(IS),2,2);

 

If yes, here, TRIM is a variable and you are replacing its first 2 bytes from he SCD array


Cheers from India!

Manjeet
Super User
Super User
Posts: 7,413

Re: Doubt in Trim portion in the below code

[ Edited ]

What is the question here?  Post eaxmple test data, in the form of a datastep, and what you expect the output to be so that we can see what the process is.  Avoid coding in capitals as this makes code very hard to read.  Use the code blocks - it is a {I} above where you post, which moves code to a proper editor area.  It looks to me like you could simplfy your if branching down to one select:

do is=1 to 500;
  select;
    when (substr(scd(is),1,1) > "V") i=500;
    when ...;
  end;
end;

Do note, I would advise not doing the substr(scd(is),1,1) > "V", that isn't what I would call logical approach.  V is a character, it works as it also has an associated ASCII code, which is what you are comparing there, but that doesn't mean that "x" would be > "V" for instance.  I would say, 

upcase(substr(scd(is),1,1)) in ("W","X","Y","Z")

would be a clearer way of writing it.

 

Edit:  One other thing to add.  It always jumps out at me when someone states they have > 50 variables in a dataset.  My first question would always be why?  There are some instances of course where it is easier, and sometimes necessary to do it this way, however I find that most times many variables makes hard work.  It may be that your logic can be simplified merely by restructuring your data into a normalised form.

Ask a Question
Discussion stats
  • 3 replies
  • 130 views
  • 2 likes
  • 4 in conversation