turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- Base SAS Programming
- /
- How to simplify the PUT procedure?

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

06-14-2017 09:05 PM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to ybz12003

06-14-2017 09:45 PM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to ybz12003

06-14-2017 09:45 PM

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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Astounding

06-14-2017 10:49 PM

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

array N {7};

array cnt {7} $ 2;

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to ybz12003

06-14-2017 11:04 PM

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;

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to ybz12003

06-14-2017 10:34 PM

Why are you converting all your numbers to character variables?

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to ybz12003

06-15-2017 02:15 AM

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

---------------------------------------------------------------------------------------------

Maxims of Maximally Efficient SAS Programmers

Maxims of Maximally Efficient SAS Programmers

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to KurtBremser

06-15-2017 07:27 AM

Thanks, I will look for the information of dictionary.columns.