Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Using a loop to set the length of string variables

    Hi all,

    Can someone help me solve the following: I am looking write a loop to set each string variable equal to the max length of each string.

    I imagine I'll need the ds command. I've tried to use the strlenght command and local the format as %`X's. Not having much luck on that.Any ideas on how I could do that?

    Here's a more detailed description:

    Suppose I have a dataset and I use the ds, has(type string). From there, I'm trying to write a loop that goes through each variable in `r(varlist)' and formats each variable equal to the maximum length. I can do this manually, using the strlen function and then formatting the variable using %s. Can anyone think of a way to do this in one loop without too much mess.

    Thanks all.
    Last edited by Justin Niakamal; 03 Aug 2017, 22:04. Reason: Edited to add more detail to my problem.

  • #2
    No loop is needed. You can do this with
    Code:
    compress

    Comment


    • #3
      Welcome to Statalist, Justin.

      You write that your objective is to

      set each string variable equal to the max length of each string.
      but then you describe changing the display format of the string, which is an entirely different concept. Consider the following.
      Code:
      . set obs 1
      number of observations (_N) was 0, now 1
      
      . generate str20 test = "A string"
      
      . describe test
      
                    storage   display    value
      variable name   type    format     label      variable label
      ------------------------------------------------------------------------------------------------
      test            str20   %20s                  
      
      . generate testlen = strlen(test)
      
      . list 
      
           +--------------------+
           |     test   testlen |
           |--------------------|
        1. | A string         8 |
           +--------------------+
      We see that test is defined as an str20 variable, meaning that it can hold up to 20 characters. It has a display format of %20s, which suggests that it will be displayed right-justified in a 20-character field. The longest string actually stored in test is 8 characters long, and in fact we see that the list command appears to have effectively used %8s as the display format.

      So it seems to me you have several possibilities for what you want.
      • Do you want to save storage by changing test from str20 to str8, as indeed the compress command will accomplish.
      • Do you want to change the display format from %20s to %8s?
      And from that, the basic question: given whatever it is you propose to do, what problem is it that you are trying to solve by doing that? Perhaps looking at the problem from another angle will yield a different solution.

      Comment


      • #4
        I thought I was making this more difficult than it needed to be. That's a useful command.

        Thanks again for your help,

        j

        Comment

        Working...
        X