BookmarkSubscribeRSS Feed
Jaji
Fluorite | Level 6
Hello,

I am working with this sas code. I have a problem: when I run the code it shows me an error message and yet I think the code is good. Please, if someone can help me pass this step. Here is the code I put.

data Base_profils;

 

/*-Macro pour la distrution aléatoire des résultats-*/

call streaminit(55);

array probFormula(9)$ _temporary_ ("F1", "F2", "F3", "F3S", "F3S+", "F4", "F5", "F5S", "F6");

array probCode_régime_1er_bénéficiaire (4) $ _temporary_ ("Alsace Moselle", "Exploitant Agricole", "Salarié", "T.N.S.");

array probCadre_légal_1er_bénéficiaire (2) $ _temporary_ ("Fonctionnaire", "Autres");

array probDépartement(97) _temporary_ (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28,

29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56,

57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84,

85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 97, 99);

array probAge_1er_bénéficiaire(70) _temporary_ (18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45,

46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73,

74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 87, 88);

array probFoyer(3)$ _temporary_ ("Seul", "Duo", "Famille");

array probAge_2nd_bénéficiaire(87) _temporary_ (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27,

28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55,

56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83,

84, 85, .);

 

/*-On défint le nombre d'observation: 5000 obs dans notre cas-*/

do i=1 to 5000;

Formule = probFormula(rand('table', 0.134, 0.055, 0.134, 0.630, 0.026, 0.007, 0.003, 0.010, 0.002));

Code_régime_1er_bénéficiaire = probCode_régime_1er_bénéficiaire(rand('table', 0.04, 0.01, 0.89, 0.06 ));

Cadre__légal_1er_bénéficiaire= probCadre_légal_1er_bénéficiaire(rand('table', 0.01 , 0.99));

Département = probDépartement(rand('table', 0.0164, 0.0124, 0.0041, 0.0007, 0.0003, 0.0225, 0.0015, 0.0069, 0.0006, 0.0040, 0.0090, 0.0042, 0.0259, 0.0087, 0.0002, 0.0020,

0.0125, 0.0020, 0.0010, 0.0002, 0.0138, 0.0099, 0.0003, 0.0062, 0.0055, 0.0041, 0.0129, 0.0066, 0.0111, 0.0109, 0.0151, 0.0011,

0.0219, 0.0189, 0.0186, 0.0031, 0.0074, 0.0238, 0.0088, 0.0080, 0.0041, 0.0156, 0.0032, 0.0143, 0.0100, 0.0002, 0.0003, 0.0001,

0.0139, 0.0078, 0.0152, 0.0074, 0.0101, 0.0158, 0.0014, 0.0110, 0.0235, 0.0025, 0.0850, 0.0083, 0.0025, 0.0433, 0.0064, 0.0133,

0.0019, 0.0087, 0.0141, 0.0127, 0.0158, 0.0032, 0.0093, 0.0090, 0.0094, 0.0164, 0.0059, 0.0300, 0.0161, 0.0120, 0.0069, 0.0230,

0.0079, 0.0027, 0.0331, 0.0107, 0.0070, 0.0080, 0.0042, 0.0089, 0.0059, 0.0015, 0.0205, 0.0076, 0.0093, 0.0100, 0.0103, 0.0002,

0.0001));

Age_1er_bénéficiaire =probAge_1er_bénéficiaire(rand('table',0.0018, 0.0031, 0.0059, 0.0062, 0.0067, 0.0049, 0.0058, 0.0065, 0.0051, 0.0051, 0.0054, 0.0065, 0.0067, 0.0052,

0.0066, 0.0075, 0.0067, 0.0062, 0.0063, 0.0049, 0.0070, 0.0058, 0.0065, 0.0056, 0.0061, 0.0071, 0.0059, 0.0079, 0.0075, 0.0077,

0.0075, 0.0084, 0.0093, 0.0086, 0.0091, 0.0098, 0.0095, 0.0113, 0.0118, 0.0107, 0.0100, 0.0112, 0.0335, 0.0443, 0.0491, 0.0498,

0.0438, 0.0470, 0.0466, 0.0408, 0.0410, 0.0395, 0.0394, 0.0329, 0.0330, 0.0327, 0.0318, 0.0198, 0.0206, 0.0148, 0.0127, 0.0107,

0.0100, 0.0014, 0.0014, 0.0011, 0.0014, 0.0035, 0.0001, 0.0001));

Foyer = probFoyer(rand('table', 0.75, 0.21, 0.05 ));

Age_2nd_bénéficiaire = probAge_2nd_bénéficiaire(rand('table', 0.0018, 0.0016, 0.0019, 0.0012, 0.0026, 0.0023, 0.0028, 0.0025, 0.0021, 0.0026, 0.0025, 0.0024, 0.0037, 0.0029,

0.0034, 0.0032, 0.0031, 0.0041, 0.0032, 0.0027, 0.0016, 0.0014, 0.0006, 0.0006, 0.0007, 0.0002, 0.0003, 0.0002, 0.0003,

0.0006, 0.0005, 0.0010, 0.0005, 0.0007, 0.0008, 0.0010, 0.0009, 0.0006, 0.0010, 0.0008, 0.0007, 0.0009, 0.0015, 0.0010,

0.0010, 0.0009, 0.0008, 0.0014, 0.0011, 0.0014, 0.0015, 0.0015, 0.0012, 0.0017, 0.0022, 0.0032, 0.0046, 0.0048, 0.0064,

0.0078, 0.0078, 0.0090, 0.0088, 0.0099, 0.0092, 0.0113, 0.0097, 0.0083, 0.0085, 0.0088, 0.0081, 0.0078, 0.0064, 0.0066,

0.0067, 0.0039, 0.0025, 0.0023, 0.0014, 0.0016, 0.0014, 0.0003, 0.0003, 0.0001, 0.0002, 0.0004, 0.7459));

output;

end;

drop i;

run;

 

13 REPLIES 13
Kurt_Bremser
Super User

The messages are very clear:

 78         array probCode_régime_1er_bénéficiaire (4) $ _temporary_ ("Alsace Moselle", "Exploitant Agricole", "Salarié",
 78       ! "T.N.S.");
 ERROR: The variable named probCode_régime_1er_bénéficiaire contains more than 32 bytes.
 79         array probCadre_légal_1er_bénéficiaire (2) $ _temporary_ ("Fonctionnaire", "Autres");
 ERROR: The variable named probCadre_légal_1er_bénéficiaire contains more than 32 bytes.
 80         array probDépartement(97) _temporary_ (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
 80       ! 23, 24, 25, 26, 27, 28,
 ERROR: The name probDépartement is not a valid SAS name.

Use Valid SAS Names.

Jaji
Fluorite | Level 6

Hi ,

 

Thank you for your reply. The problem is at the level of the variable 'Age_2nd_beneficiary' because when I run the program without this variable, the code works fine. Here is the error message it displays to me.

ERROR: Array subscript out of range at line 68 column 24.

 

 

Kurt_Bremser
Super User

@Jaji wrote:

Hi ,

 

Thank you for your reply. The problem is at the level of the variable 'Age_2nd_beneficiary' because when I run the program without this variable, the code works fine. Here is the error message it displays to me.

ERROR: Array subscript out of range at line 68 column 24.

 

 


NO.WAY.

Your code as posted creates this log:

 73         data Base_profils;
 74         
 75         /*-Macro pour la distrution aléatoire des résultats-*/
 76         call streaminit(55);
 77         array probFormula(9)$ _temporary_ ("F1", "F2", "F3", "F3S", "F3S+", "F4", "F5", "F5S", "F6");
 78         array probCode_régime_1er_bénéficiaire (4) $ _temporary_ ("Alsace Moselle", "Exploitant Agricole", "Salarié",
 78       ! "T.N.S.");
 ERROR: The variable named probCode_régime_1er_bénéficiaire contains more than 32 bytes.
 79         array probCadre_légal_1er_bénéficiaire (2) $ _temporary_ ("Fonctionnaire", "Autres");
 ERROR: The variable named probCadre_légal_1er_bénéficiaire contains more than 32 bytes.
 80         array probDépartement(97) _temporary_ (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
 80       ! 23, 24, 25, 26, 27, 28,
 ERROR: The name probDépartement is not a valid SAS name.
 81         29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56,
 82         57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84,
 83         85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 97, 99);
 84         array probAge_1er_bénéficiaire(70) _temporary_ (18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
 84       !  36, 37, 38, 39, 40, 41, 42, 43, 44, 45,
 ERROR: The name probAge_1er_bénéficiaire is not a valid SAS name.
 85         46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73,
 86         74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 87, 88);
 87         array probFoyer(3)$ _temporary_ ("Seul", "Duo", "Famille");
 88         array probAge_2nd_bénéficiaire(87) _temporary_ (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
 88       ! 20, 21, 22, 23, 24, 25, 26, 27,
 ERROR: The name probAge_2nd_bénéficiaire is not a valid SAS name.
 89         28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55,
 90         56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83,
 91         84, 85, .);
 92         
 93         /*-On défint le nombre d'observation: 5000 obs dans notre cas-*/
 94         do i=1 to 5000;
 95         Formule = probFormula(rand('table', 0.134, 0.055, 0.134, 0.630, 0.026, 0.007, 0.003, 0.010, 0.002));
 96         Code_régime_1er_bénéficiaire = probCode_régime_1er_bénéficiaire(rand('table', 0.04, 0.01, 0.89, 0.06 ));
 ERROR: The name Code_régime_1er_bénéficiaire is not a valid SAS name.
 ERROR: The variable named probCode_régime_1er_bénéficiaire contains more than 32 bytes.
 97         Cadre__légal_1er_bénéficiaire= probCadre_légal_1er_bénéficiaire(rand('table', 0.01 , 0.99));
 ERROR: The name Cadre__légal_1er_bénéficiaire is not a valid SAS name.
 ERROR: The variable named probCadre_légal_1er_bénéficiaire contains more than 32 bytes.
 98         Département = probDépartement(rand('table', 0.0164, 0.0124, 0.0041, 0.0007, 0.0003, 0.0225, 0.0015, 0.0069, 0.0006,
 98       ! 0.0040, 0.0090, 0.0042, 0.0259, 0.0087, 0.0002, 0.0020,
 ERROR: The name Département is not a valid SAS name.
 ERROR: The name probDépartement is not a valid SAS name.
 99         0.0125, 0.0020, 0.0010, 0.0002, 0.0138, 0.0099, 0.0003, 0.0062, 0.0055, 0.0041, 0.0129, 0.0066, 0.0111, 0.0109, 0.0151,
 99       ! 0.0011,
 100        0.0219, 0.0189, 0.0186, 0.0031, 0.0074, 0.0238, 0.0088, 0.0080, 0.0041, 0.0156, 0.0032, 0.0143, 0.0100, 0.0002, 0.0003,
 100      ! 0.0001,
 101        0.0139, 0.0078, 0.0152, 0.0074, 0.0101, 0.0158, 0.0014, 0.0110, 0.0235, 0.0025, 0.0850, 0.0083, 0.0025, 0.0433, 0.0064,
 101      ! 0.0133,
 102        0.0019, 0.0087, 0.0141, 0.0127, 0.0158, 0.0032, 0.0093, 0.0090, 0.0094, 0.0164, 0.0059, 0.0300, 0.0161, 0.0120, 0.0069,
 102      ! 0.0230,
 103        0.0079, 0.0027, 0.0331, 0.0107, 0.0070, 0.0080, 0.0042, 0.0089, 0.0059, 0.0015, 0.0205, 0.0076, 0.0093, 0.0100, 0.0103,
 103      ! 0.0002,
 104        0.0001));
 105        Age_1er_bénéficiaire =probAge_1er_bénéficiaire(rand('table',0.0018, 0.0031, 0.0059, 0.0062, 0.0067, 0.0049, 0.0058,
 105      ! 0.0065, 0.0051, 0.0051, 0.0054, 0.0065, 0.0067, 0.0052,
 ERROR: The name Age_1er_bénéficiaire is not a valid SAS name.
 ERROR: The name probAge_1er_bénéficiaire is not a valid SAS name.
 106        0.0066, 0.0075, 0.0067, 0.0062, 0.0063, 0.0049, 0.0070, 0.0058, 0.0065, 0.0056, 0.0061, 0.0071, 0.0059, 0.0079, 0.0075,
 106      ! 0.0077,
 107        0.0075, 0.0084, 0.0093, 0.0086, 0.0091, 0.0098, 0.0095, 0.0113, 0.0118, 0.0107, 0.0100, 0.0112, 0.0335, 0.0443, 0.0491,
 107      ! 0.0498,
 108        0.0438, 0.0470, 0.0466, 0.0408, 0.0410, 0.0395, 0.0394, 0.0329, 0.0330, 0.0327, 0.0318, 0.0198, 0.0206, 0.0148, 0.0127,
 108      ! 0.0107,
 109        0.0100, 0.0014, 0.0014, 0.0011, 0.0014, 0.0035, 0.0001, 0.0001));
 110        Foyer = probFoyer(rand('table', 0.75, 0.21, 0.05 ));
 111        Age_2nd_bénéficiaire = probAge_2nd_bénéficiaire(rand('table', 0.0018, 0.0016, 0.0019, 0.0012, 0.0026, 0.0023, 0.0028,
 111      !  0.0025, 0.0021, 0.0026, 0.0025, 0.0024, 0.0037, 0.0029,
 ERROR: The name Age_2nd_bénéficiaire is not a valid SAS name.
 ERROR: The name probAge_2nd_bénéficiaire is not a valid SAS name.
 112        0.0034, 0.0032, 0.0031, 0.0041, 0.0032, 0.0027, 0.0016, 0.0014, 0.0006, 0.0006, 0.0007, 0.0002, 0.0003, 0.0002, 0.0003,
 113        0.0006, 0.0005, 0.0010, 0.0005, 0.0007, 0.0008, 0.0010, 0.0009, 0.0006, 0.0010, 0.0008, 0.0007, 0.0009, 0.0015, 0.0010,
 114        0.0010, 0.0009, 0.0008, 0.0014, 0.0011, 0.0014, 0.0015, 0.0015, 0.0012, 0.0017, 0.0022, 0.0032, 0.0046, 0.0048, 0.0064,
 115        0.0078, 0.0078, 0.0090, 0.0088, 0.0099, 0.0092, 0.0113, 0.0097, 0.0083, 0.0085, 0.0088, 0.0081, 0.0078, 0.0064, 0.0066,
 116        0.0067, 0.0039, 0.0025, 0.0023, 0.0014, 0.0016, 0.0014, 0.0003, 0.0003, 0.0001, 0.0002, 0.0004, 0.7459));
 117        output;
 118        end;
 119        drop i;
 120        run;
 
 NOTE: The SAS System stopped processing this step because of errors.

So you see there are numerous ERRORs because of the invalid names. Fixing just one won't make your code run, period.

If you fixed all names, please post the corrected code.

Use this button for posting logs:

Bildschirmfoto 2020-04-07 um 08.32.59.jpg

and the "little running man" right next to it for SAS code.

FreelanceReinh
Jade | Level 19

Hallo @Kurt_Bremser,

 

With option validvarname=any my Windows SAS (reluctantly) accepts all these variable and array names (and only the "Array subscript out of range" message is issued). Without the option I get the expected "... is not a valid SAS name" errors, but not your "... contains more than 32 bytes" messages. I suspect that your system (encoding) counts the French characters such as "é" as two (or more) bytes, which then exceeds the length limit in cases with (close to) 32 characters, e.g. in probCadre_légal_1er_bénéficiaire.

Kurt_Bremser
Super User

I take it that the OP works with Windows codepage 1252, where the usual Western Europe characters (like the accented e) are available as single-byte characters. But University Edition (what I use to test code) runs with UTF-8, where those characters need to be transcoded and expanded, leading to the problem. Another reason for NOT making use of VALIDVARNAME=ANY.

Tom
Super User Tom
Super User

You are talking apples and oranges here. 

 

The code does not run in your UTF-8 session because the UTF-8 encoding of those variable names takes more than 32 bytes.  It might be the original poster used some single byte encoding where those names were not longer than 32 bytes.

Kurt_Bremser
Super User

@Tom wrote:

You are talking apples and oranges here. 

 

The code does not run in your UTF-8 session because the UTF-8 encoding of those variable names takes more than 32 bytes.  It might be the original poster used some single byte encoding where those names were not longer than 32 bytes.


It may not be obvious as I am not a native English speaker, but that's exactly what I meant. The OP uses a single-byte system with the typical WLATIN1 (Windows 1252) codepage, and the transcoding of the variable names (that probably happens when I copy/paste from the communities to SAS Studio, all within Safari) cracks the 32-character barrier.

andreas_lds
Jade | Level 19

Please post the log using "insert code" button - without the exact error message an the context it is hardly possible to help.

Jaji
Fluorite | Level 6
Hello again, do you have an e-mail address so I will send you the airlock program and the journal so that you can see more clearly. The I do not know how to insert the code and the log on the page.
andreas_lds
Jade | Level 19

If you want help, post the required information or hire someone solving the problems.

FreelanceReinh
Jade | Level 19

Hello @Jaji,

 

The issue is that five of the the seven sums of probabilities specified in rand('table', ...) calls differ from 1, most likely due to rounding errors: 1.001, 1, 1, 1.0006, 1.0002, 1.01, 0.9997. In the latter case (0.9997) this means that with probability 1-0.9997=0.0003 a value of 88 is generated, which exceeds the defined array dimension (87), hence the error message.

 

In some of the other cases (with sums >1) a few of the last array elements will never be selected because after hitting the cumulative sum 1 they get probability zero. This error is dangerous because there is no corresponding log message.

 

So, you need to correct the probabilities. I would store them in one or more SAS datasets (and extract them from there) and thus avoid typing all those numbers into the code.

 

All that said, I second the recommendation to use shorter variable and array names without non-English characters ("é", etc.).

Jaji
Fluorite | Level 6

Hi KurtBremser,

 

Thank you for your reply.

 

I understand what your talk about. I will do this.

 

Thank your and have a good day

FreelanceReinh
Jade | Level 19

Here is a simplified example of how you can avoid the "Array subscript out of range" error:

 

Suppose you want to simulate 5000 observations with an age distribution similar to that of the girls in SASHELP.CLASS. Your current approach might look like this:

Look at the output of

proc freq data=sashelp.class;
where sex='F';
tables age;
run;
The FREQ Procedure

                                Cumulative    Cumulative
Age    Frequency     Percent     Frequency      Percent
--------------------------------------------------------
 11           1       11.11             1        11.11
 12           2       22.22             3        33.33
 13           2       22.22             5        55.56
 14           2       22.22             7        77.78
 15           2       22.22             9       100.00

and enter the age values and (rounded!) relative frequencies (i.e. percentages/100) manually into the code as shown in the log below:

1    data doesntwork;
2    call streaminit(55);
3    array probAge[5] _temporary_ (11 12 13 14 15);
4    do i=1 to 5000;
5      Age = probAge[rand('table', 0.1111, 0.2222, 0.2222, 0.2222, 0.2222)];
6      output;
7    end;
8    drop i;
9    run;

ERROR: Array subscript out of range at line 5 column 9.
i=4548 Age=12 _ERROR_=1 _N_=1
NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.DOESNTWORK may be incomplete.  When this step was stopped there were 4547 observations and 1 variables.
NOTE: DATA statement used (Total process time):
      real time           0.02 seconds
      cpu time            0.01 seconds

Problem: After 4547 observations with array subscripts within the range 1 - 5 the RAND function happened to generate the value 6 because it was allowed to: 0.1111+4*0.2222<1.

 

Improved approach avoiding this issue:

/* Store categories and probabilities (or percentages) in a dataset (here: CNT) */

proc freq data=sashelp.class;
where sex='F';
tables age / out=cnt;
run;

/* Extract values, their number and probabilities (with high precision) into macro variables */

proc sql noprint;
select count(age), age, percent/100 format=best24.
  into :n_ages trimmed,
       :ages   separated by ' ',
       :probs  separated by ','
from cnt
order by age;
quit;

/* Check the values */

%put &=n_ages;
%put &=ages;
%put &=probs;

/* Use these macro variables in array definition and RAND function call */ 

data want;
call streaminit(55);
array probAge[&n_ages] _temporary_ (&ages);
do i=1 to 5000;
  Age = probAge[rand('table', &probs)];
  output;
end;
drop i;
run;

If AGE was a character variable, say, of length 12, two changes would be necessary:

select count(age), quote(age), percent/100 format=best24.
  • Adapt the array definition as usual:
array probAge[&n_ages] $12 _temporary_ (&ages);

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 13 replies
  • 758 views
  • 2 likes
  • 5 in conversation