I have these variables: Q20_1_1 Q20_1_2 Q20_1_3 Q20_1_4 Q20_1_5 Q36_1_1 Q36_1_2 Q36_1_3 Q36_1_4 Q36_1_5
that need to be redefined as, for an example:
if Q20_1_1="Never" then Q20_1_1a="Never";
if Q20_1_1="Once in past 12 months" then Q20_1_1a="Rarely";
if Q20_1_1="Two to five times in past 12 months" then Q20_1_1a="Sometimes";
if Q20_1_1="Every other month to monthly" then Q20_1_1a="Often";
if Q20_1_1="A few times a month to weekly" or Q20_1_1="A few times a week to daily" then Q20_1_1a="Very often";
How can I create an array for this?
Sounds more like you want a format. Then you could probably avoid creating new variables.
proc format ;
value $freqa
"Never"="Never"
"Once in past 12 months"="Rarely"
"Two to five times in past 12 months"="Sometimes"
"Every other month to monthly"="Often"
"A few times a month to weekly"
,"A few times a week to daily"="Very often"
;
run;
Then you could just attach that format to your existing variable.
proc freq data=have ;
tables Q20_1_1 Q20_1_2 Q20_1_3 Q20_1_4 Q20_1_5 Q36_1_1 Q36_1_2 Q36_1_3 Q36_1_4 Q36_1_5 ;
format Q20_1_1 Q20_1_2 Q20_1_3 Q20_1_4 Q20_1_5 Q36_1_1 Q36_1_2 Q36_1_3 Q36_1_4 Q36_1_5 $freqa. ;
run;
If you really did need to make new variables then you two arrays. One for the original and one for the new variables.
data want ;
set have ;
array in Q20_1_1 Q20_1_2 Q20_1_3 Q20_1_4 Q20_1_5 Q36_1_1 Q36_1_2 Q36_1_3 Q36_1_4 Q36_1_5 ;
array out $20 Q20_1_1a Q20_1_2a Q20_1_3a Q20_1_4a Q20_1_5a Q36_1_1a Q36_1_2a Q36_1_3a Q36_1_4a Q36_1_5a ;
do i=1 to dim(in);
out(i)=put(in(i),$freqa.);
end;
run;
Sounds more like you want a format. Then you could probably avoid creating new variables.
proc format ;
value $freqa
"Never"="Never"
"Once in past 12 months"="Rarely"
"Two to five times in past 12 months"="Sometimes"
"Every other month to monthly"="Often"
"A few times a month to weekly"
,"A few times a week to daily"="Very often"
;
run;
Then you could just attach that format to your existing variable.
proc freq data=have ;
tables Q20_1_1 Q20_1_2 Q20_1_3 Q20_1_4 Q20_1_5 Q36_1_1 Q36_1_2 Q36_1_3 Q36_1_4 Q36_1_5 ;
format Q20_1_1 Q20_1_2 Q20_1_3 Q20_1_4 Q20_1_5 Q36_1_1 Q36_1_2 Q36_1_3 Q36_1_4 Q36_1_5 $freqa. ;
run;
If you really did need to make new variables then you two arrays. One for the original and one for the new variables.
data want ;
set have ;
array in Q20_1_1 Q20_1_2 Q20_1_3 Q20_1_4 Q20_1_5 Q36_1_1 Q36_1_2 Q36_1_3 Q36_1_4 Q36_1_5 ;
array out $20 Q20_1_1a Q20_1_2a Q20_1_3a Q20_1_4a Q20_1_5a Q36_1_1a Q36_1_2a Q36_1_3a Q36_1_4a Q36_1_5a ;
do i=1 to dim(in);
out(i)=put(in(i),$freqa.);
end;
run;
Then mark it as a solution.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.