## How to simplify the PUT procedure?

Solved
Super Contributor
Posts: 394

# How to simplify the PUT procedure?

Hello:

I am wondering if there is a way to avoid writing such long 'PUT" statement below?  Also, I list another question below.  Thanks.

data test1;
input N1-N7;
cards;
1 1 1 1 1 1 1
1 0 1 1 0 0 1
0 0 0 0 1 1 1
1 1 1 1 1 1 1
;
run;

proc means data=test1;
var _numeric_;
output out=test2 sum=n1-n7; /*Is there another way to list from N1 to N7 so that no need to type? */
run;

data test3;
set test2 (keep=N1-N7 obs=1);
cnt1=put(n1,2.);
cnt2=put(n2,2.);
cnt3=put(n3,2.);
cnt4=put(n4,2.);
cnt5=put(n5,2.);
cnt6=put(n6,2.);
cnt7=put(n7,2.); /*Is there an anther way to do this instead of listing all of numbers? I wish I don't need to count.*/
run;

Accepted Solutions
Solution
‎06-15-2017 07:26 AM
Super User
Posts: 6,629

## Re: How to simplify the PUT procedure?

Q1. When PROC MEANS calculates just one type of statistic, such as the SUM, you can specify:

output out=test2 sum=;

By skipping the naming of the variables, this tells SAS to re-use the original names.  So TEST2 contains the original variable names, but holding the SUM of the original values.

Q2. When you process a set of variables using the same logic, you can use arrays.  In this case, since you only have 7 variables, the replacement code is nearly as long as the original:

data test3;
set test2 (keep=N1-N7 obs=1);

array N {7};

array cnt {7} \$ 2;

do _n_=1 to 7;

cnt{_n_} = put(N{_n_}, 2.);

end;

run;

So it's more complex (you will need to learn about arrays), and nearly as long as the original.  But if you had 300 variables instead of 7, the length of the program wouldn't change.  And it doesn't save you counting.  You have to add the "7" as part of the ARRAY statement.

All Replies
Solution
‎06-15-2017 07:26 AM
Super User
Posts: 6,629

## Re: How to simplify the PUT procedure?

Q1. When PROC MEANS calculates just one type of statistic, such as the SUM, you can specify:

output out=test2 sum=;

By skipping the naming of the variables, this tells SAS to re-use the original names.  So TEST2 contains the original variable names, but holding the SUM of the original values.

Q2. When you process a set of variables using the same logic, you can use arrays.  In this case, since you only have 7 variables, the replacement code is nearly as long as the original:

data test3;
set test2 (keep=N1-N7 obs=1);

array N {7};

array cnt {7} \$ 2;

do _n_=1 to 7;

cnt{_n_} = put(N{_n_}, 2.);

end;

run;

So it's more complex (you will need to learn about arrays), and nearly as long as the original.  But if you had 300 variables instead of 7, the length of the program wouldn't change.  And it doesn't save you counting.  You have to add the "7" as part of the ARRAY statement.

Super Contributor
Posts: 394

## Re: How to simplify the PUT procedure?

I'm wondering, is there a way not typing 7 after array?

array N {7};

array cnt {7} \$ 2;

Super User
Posts: 6,629

## Re: How to simplify the PUT procedure?

There are variations available on the ARRAY statement (you might have some studying to do).  But you have to type a 7 somewhere.  This variation would be possible:

array N {*} N1-N7;

Super User
Posts: 23,262

## Re: How to simplify the PUT procedure?

Why are you converting all your numbers to character variables?

Super User
Posts: 9,886

## Re: How to simplify the PUT procedure?

You can extract a count of variables from dictionary.columns.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super Contributor
Posts: 394