Help using Base SAS procedures

Array for redefining variables

Accepted Solution Solved
Reply
Contributor
Posts: 32
Accepted Solution

Array for redefining variables

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?

 


Accepted Solutions
Solution
‎10-09-2017 03:20 PM
Super User
Super User
Posts: 8,069

Re: Array for redefining variables

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;

View solution in original post


All Replies
Solution
‎10-09-2017 03:20 PM
Super User
Super User
Posts: 8,069

Re: Array for redefining variables

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;
Contributor
Posts: 32

Re: Array for redefining variables

Thank you-that worked!!
Trusted Advisor
Posts: 1,337

Re: Array for redefining variables

Then mark it as a solution.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 233 views
  • 1 like
  • 3 in conversation