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

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?

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

3 REPLIES 3
Tom
Super User Tom
Super User

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;
lboyd
Calcite | Level 5
Thank you-that worked!!
mkeintz
PROC Star

Then mark it as a solution.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

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

sas-innovate-2024.png

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.

 

Register now!

What is Bayesian Analysis?

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.

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
  • 3 replies
  • 1064 views
  • 1 like
  • 3 in conversation