## Proc Means for multiple variables with conditional statement

Hello,

I am currently having trouble figuring out how to combine multiple variables into one large grouped variables while still maintaining the values of the variables constant. Another issue I have is that I need to conduct a total sum of all the variables grouped by an identifier number but only have scores summed if it more than 5 out of 9 variables have data. For the first question, I want to combine 9 variables called SCNS1, SCNS2, etc.. into one grouped variable so that I can, in the future, compare that grouped variable with another grouped variable. For the second issue, I want to get the sum of SCNS1-9 grouped by identifier number only if 5 of the 9 variables have been filled.

A code that I thought would help me for the second issue is:

if n(of scns1-scns9)>=5 then sum=sum(of scns1-scns9);

8 REPLIES 8

## Re: Proc Means for multiple variables with conditional statement

Please generate some sample data and show the issue with data.
Example input data and sample expected output that matches with the input really helps to convey the problem more clearly.

## Re: Proc Means for multiple variables with conditional statement

Actually, is it possible for the 2nd row to have the missing data inputted by the mean of the available data in the second row? So in row 2, instead of 3 missing values, it is filled out with the average mean of the actual completed data in row 2? The average of the values in row 2 would be 3.83 so SAS would replace the missing values in row 2 with that value.

Sample data:

SCNS1 SCNS2 SCNS3 SCNS4 SCNS5 SCNS6 SCNS7 SCNS8 SCNS9

1             2             3            4            5          5           5           5         5

2             5            3              4           . (3.83)   .(3.83)  .(3.83)  4         5

.                .          .              .               .          .             .            .           .

Desired output:

Totalscore

35

34.5

.

## Re: Proc Means for multiple variables with conditional statement

This question was asked on Reddit in the last week or so. Since Reddit is closed today, I can't give you a link.

However, I now assume that this is a homework assignment (since two different people have asked this question), and so you will need to show us what you have tried, rather than expecting us to just code something up for you.

--
Paige Miller

## Re: Proc Means for multiple variables with conditional statement

For the first question, I want to combine 9 variables called SCNS1, SCNS2, etc.. into one grouped variable so that I can, in the future, compare that grouped variable with another grouped variable

This sort of thing is so data dependent that I wouldn't even make a suggestion other than sorting values is might one of the first things involved.  Examples of actual values , both your source and of the "other" grouped variable are needed.

I am not sure where Proc means enters into this at all.

A code that I thought would help me for the second issue is:

if n(of scns1-scns9)>=5 then sum=sum(of scns1-scns9);

Did you test that? Did it not work?

## Re: Proc Means for multiple variables with conditional statement

I am a SAS newbie so I am sorry for my questions. How would I go about testing for the code?

## Re: Proc Means for multiple variables with conditional statement

@fiarwizord wrote:
I am a SAS newbie so I am sorry for my questions. How would I go about testing for the code?

You have data. Run a data step with that code in it and compare the results to see if does what you expect.

```Data test;
set have; /* the name of your existing data set goes here*/
if n(of scns1-scns9)>=5 then scns_sum=sum(of scns1-scns9);
run;```

I named the result variable scns_sum instead of Sum because it easy to lose track of what a variable is supposed to hold with a name like "sum". It is not syntactically incorrect to use SAS functions as variable names but in some places notes in the log make your log not "clean" and may result with a few procedures in unexpected results if not careful.

If you only want to test a few records out of a large data set use the OBS= option to specify a number of observations to use:

`   set have (obs=25);`

Will use only the first 25 observations from the data set Have.

## Re: Proc Means for multiple variables with conditional statement

Thank you so much for the replies! I was able to get through the hurdle of my issue. I have a quick question on how to recode numeric variables. Let us say a variable has values of 1-5 but I want to make it so that a value of 5 would equal 4, a value of 4 would equal 3, etc.. I tried recoding it in the data step but it returns to me values of 0 for my variable since I am assuming it continuously recoded it until it hits the lowest value.

## Re: Proc Means for multiple variables with conditional statement

@fiarwizord wrote:
Thank you so much for the replies! I was able to get through the hurdle of my issue. I have a quick question on how to recode numeric variables. Let us say a variable has values of 1-5 but I want to make it so that a value of 5 would equal 4, a value of 4 would equal 3, etc.. I tried recoding it in the data step but it returns to me values of 0 for my variable since I am assuming it continuously recoded it until it hits the lowest value.

Sounds like a simple subtraction operation.

``new = old - 1;``

Where does 1 (or 0 or whatever the lowerbound is) go?

If your code is not working you need show us the code.

One thing to avoid is code that changes its own inputs.

``````data have;
set have;
oldvar = oldvar -1 ;
run;``````

If you run that twice in a row it is like running this once.

``````data have;
set have;
oldvar = oldvar -2 ;
run;``````

Create NEW datasets and/or create NEW variables.

From SAS Users blog