BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Hello_there
Lapis Lazuli | Level 10

Hello, the purpose of this is data manipulation practice.

 

I'm trying to use an array to count up the X's in the val1, val2, val3 variables. I'm trying to count across.

 

data have;
infile datalines dsd dlm=",";
 	input subject $ val1 $ val2 $ val3 $;
 datalines;
 001, x, , ,
 002, x, x, ,
 003, x, , , 
 004, , , ,
 005, x, x, x
 ; 
 run;
 

results should be:

 

subject sum

001        1

002      2

003       1

004      0

005     3

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

@Hello_there wrote:
Thanks for the quick solution. I would like to use an array bc it would make it more flexible for when i re run the code for different variable amounts.

That would allow you to use the DIM() function.

array list var1-var3;
sum=dim(list)-cmiss(of list[*]);

View solution in original post

6 REPLIES 6
Tom
Super User Tom
Super User

No need for an array.

data want;
  set have;
  sum = 3 - cmiss(of var1-var3);
  keep subject sum;
run;
Hello_there
Lapis Lazuli | Level 10
Thanks for the quick solution. I would like to use an array bc it would make it more flexible for when i re run the code for different variable amounts.
Tom
Super User Tom
Super User

@Hello_there wrote:
Thanks for the quick solution. I would like to use an array bc it would make it more flexible for when i re run the code for different variable amounts.

That would allow you to use the DIM() function.

array list var1-var3;
sum=dim(list)-cmiss(of list[*]);
ballardw
Super User

@Hello_there wrote:
Thanks for the quick solution. I would like to use an array bc it would make it more flexible for when i re run the code for different variable amounts.

Provide a more realistic set of values then. And maybe variable names. Many of the functions that do some of this sort of stuff will work with a variable list, such as @Tom 's CMISS example.

There are functions that work well for single character values in a similar manner, Countc. But if you specific words then examples are needed.

 

The basic approach if you really need to examine each value is going to look like:

Counter=0;
do i= 1 to dim(arrayname);
   counter = sum(counter,<condition involving arrayname[i]>);
end;

What that condition may look like depends a whole lot on the values.

Hello_there
Lapis Lazuli | Level 10
Understandable.

For my use case, the example i provided is the general structure of the problem i encounter a lot. The only things that needs to change is the amount of variables i put in the array.

That being said, i will consider your approach also. Thanks

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
  • 6 replies
  • 550 views
  • 4 likes
  • 3 in conversation