BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ybz12003
Rhodochrosite | Level 12

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;

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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.

 

View solution in original post

6 REPLIES 6
Astounding
PROC Star

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.

 

ybz12003
Rhodochrosite | Level 12

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

 

array N {7};

array cnt {7} $ 2;

Astounding
PROC Star

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;

 

Reeza
Super User

Why are you converting all your numbers to character variables? 

 

ybz12003
Rhodochrosite | Level 12

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 803 views
  • 3 likes
  • 4 in conversation