BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
joebacon
Pyrite | Level 9

Hi all, 

 

It has been a while. I am having issues with trying to count the number of values across a row that are greater than or equal to 6. The issue arises when I go to put in the variables for the array and it seems that SAS doesn't want to count the leading 0 in the variable name. Rather than recoding each variable, I was wondering if there is an easy way to fix my problem. Alternatively, is there an easy way to recode all of the variable names to something easier for the array to deal with. The code I am attempting to utilize is a simple array.

 

data Count;

set Employment2;

array a{*}  E0011701-E001752;

values_gt_6=0;

do _n_=1 to dim(a);

if a{_n_}>5 then values_gt_6+1;

end;

run;

What this does is create new variable E011701 through E001752 in that order with all values being null. It also leaves the values_gt_6 null as well.

 

Any help would be appreciated.

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Why is the first name 8 characters long and the second only 7?

E0011701
E001752

That doesn't look like a set of variables with a numeric suffix to me.

 

SAS is interpreting that as a request for 9.950 variables:

95    data employment2 ;
96     length E0011701-E001752 8;
97    run;

NOTE: Variable E011701 is uninitialized.
NOTE: Variable E011700 is uninitialized.
...
NOTE: Variable E001753 is uninitialized.
NOTE: Variable E001752 is uninitialized.
NOTE: The data set WORK.EMPLOYMENT2 has 1 observations and 9950 variables.

View solution in original post

7 REPLIES 7
Tom
Super User Tom
Super User

Why is the first name 8 characters long and the second only 7?

E0011701
E001752

That doesn't look like a set of variables with a numeric suffix to me.

 

SAS is interpreting that as a request for 9.950 variables:

95    data employment2 ;
96     length E0011701-E001752 8;
97    run;

NOTE: Variable E011701 is uninitialized.
NOTE: Variable E011700 is uninitialized.
...
NOTE: Variable E001753 is uninitialized.
NOTE: Variable E001752 is uninitialized.
NOTE: The data set WORK.EMPLOYMENT2 has 1 observations and 9950 variables.
joebacon
Pyrite | Level 9
You are absolutely correct. One is only 7 characters. I felt like I checked that 3-4 times because that was the most logical explanation. I appreciate the quick response and I apologize for my oversight.
Tom
Super User Tom
Super User

You can use variables that start with a prefix:

array a E00: ;

Or based on position in the dataset.

array a E0011701 -- E001752

Even just the numeric variables in that position range.

array a E0011701 -numeric- E001752
joebacon
Pyrite | Level 9
Tom, this was super insightful. I didn't know that the array methods were so robust. I definitely will utilize this in the future.
joebacon
Pyrite | Level 9

@Tom A follow up question:

 

If all the values in the array are missing, will it output the value of "values_gt_6" as missing? If not, is there a way to do that within the same data step?

Tom
Super User Tom
Super User

@joebacon wrote:

@Tom A follow up question:

 

If all the values in the array are missing, will it output the value of "values_gt_6" as missing? If not, is there a way to do that within the same data step?


Probably, but it would be simpler to just test for that separately.

if 0=n(of A[*]) then values_gt_6=.;
ballardw
Super User

First thing, what are the actual names of your existing variables?

Since you say " create new variable E011701 through E001752" that means those variables did not exist, at least to me. ( and suspect a type where that should be E0011701 or your posted data step code is the typo).

 

If you mention variable names on an Array statement that do not exist then the Array statement will create the variables. Been that way for long time.

 

If your actual variables are adjacent in the data step you may be able to use the -- (two dash) list creator. You place the left most (or lowest variable number as reported by Proc Contents) before the -- and last adjacent after it.

Example: If you have variables of the same type that are in positions 15 through 30 in your data set and the one at 15 is named ABC and the 30th is named PDQ then use:  Array a{*} ABC -- PDQ; to have all of those variables in the array.

 

 

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 7 replies
  • 481 views
  • 3 likes
  • 3 in conversation