Page 1 of 1

DECLARE LIST OF STRING VARIABLES

Posted: January 18th, 2019, 3:04 am
by col Ar
Dear all,
I would like to declare a string array taking 60 characters
I have done the following:

Code: Select all

array string myString(100), myString2(100), myString3(100);
However, it fails to compile except when I declare only 1 string as:

Code: Select all

array string myString(100);
Can someone help please?

Re: DECLARE LIST OF STRING VARIABLES

Posted: January 18th, 2019, 5:53 am
by khurshid.arshad
your declaration maybe look like this:

array string(100) myString(3); {3 names, each up to 100 characters long}

example from help

Code: Select all

Example 2: (alphanumeric array)

PROC GLOBAL
  array alpha(10) crop (20); {20 crop names, each up to 10 chars long}

PROC MY_PROGRAM
preproc
  crop(1)= "maize";
  crop(2)= "wheat";
  crop(3)= "rice";
  crop(4)= "potatoes";

Best.
a.

Re: DECLARE LIST OF STRING VARIABLES

Posted: January 18th, 2019, 8:19 am
by josh
Just put each one a separate line:
array string myString(100);
array string myString2(100);
array string myString3(100);
Note that a string is variable length so it can have as many characters as you want. So

Code: Select all

array string myString(100)
is an array of 100 strings not a string of length 100.

With the old alpha type that you see in Khurshid's example you had to specify the length. But you should always use string rather than the alpha type. alpha is a leftover from early versions of CSPro that didn't have variable length strings.

Re: DECLARE LIST OF STRING VARIABLES

Posted: January 20th, 2019, 11:24 am
by col Ar
That's so clear. Thanks for the clarification

Re: DECLARE LIST OF STRING VARIABLES

Posted: January 20th, 2019, 11:33 am
by col Ar
So, how do I declare those 3 stings as I want them each to have a length of 100,not 100 strings as @Josh has explained. My aim is to put them in same line coz I have other strings that take different lengths

Re: DECLARE LIST OF STRING VARIABLES

Posted: January 21st, 2019, 7:57 am
by josh
The whole point of string variables is you do not need to specify the length. The system makes them as long you need them to be based on what values you assign to them. So you don't need to specify the length at all.

Re: DECLARE LIST OF STRING VARIABLES

Posted: January 22nd, 2019, 1:06 pm
by Gregory Martin
This, as an example, declares an array of 100 alpha objects where each is 60 characters:
array alpha(60) my_array_name(100);
As Josh mentioned, we suggest that you use strings instead of alphas. If you are concerned about how a report looks when written out, you can control the width of the strings when written out. For example:
write("%-60s", my_string_name);
That will pad the string so that it is left-justified and takes up 60 characters. See more here:

http://www.csprousers.org/help/CSPro/me ... tions.html

Re: DECLARE LIST OF STRING VARIABLES

Posted: January 23rd, 2019, 3:30 am
by col Ar
This is well understood