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;
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.
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.
I'm wondering, is there a way not typing 7 after array?
array N {7};
array cnt {7} $ 2;
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;
Why are you converting all your numbers to character variables?
You can extract a count of variables from dictionary.columns.
Thanks, I will look for the information of dictionary.columns.
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.