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

Variables Q1-Q200 has numeric Values 1, 2, 3. Based on Questionnaire. 1=Yes, 

2=No, 3=Maybe. How would you assign to 200 new variables (QX1-QX200) the 

character equivalent of the 200 old variables without having to write 200+ lines of 

Code?

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

You need to use ARRAY statement to allow you to perform the same operation on many variables.

Assuming they are all using the same code list then just use a PUT() function with the format to make the value to text conversion.

data want;
  set have ;
  array old Q1-Q2000;
  array new $10 QX1-QX2000;
  do i=1 to dim(old);
    new=put(old(i),yesno.);
  end;
run;

If they different formats then use the VVALUE() function instead to get the formatted value of the variable based on whatever format the variable already has attached.  You might need to use the LEFT() function also to remove leading spaces in the formatted value, depending on how you want your character variables created.

 

As @Kurt_Bremser sasys you also might want to switch to a vertical format:

 

data tall;
  set have ;
  array old Q1-Q2000;
  length qnum 8 qname $32 code 8 decode $32 ;
  do qnum=1 to dim(old);
    qname=vname(old(qnum));
    code=old(qnum);
    decode=vvaluex(old(qnum));
    output;
  end;
  drop q1-q2000;
run;

View solution in original post

9 REPLIES 9
andreas_lds
Jade | Level 19

@BartlomiejR wrote:

Variables Q1-Q200 has numeric Values 1, 2, 3. Based on Questionnaire. 1=Yes, 

2=No, 3=Maybe. How would you assign to 200 new variables (QX1-QX200) the 

character equivalent of the 200 old variables without having to write 200+ lines of 

Code?


Why are the variables numeric? You should have imported them as char. If you can't fix the import, use the search facility in the community. This questions has been asked and answered to many times.

BartlomiejR
Calcite | Level 5

Im not after the changing statement but just want to know how can apply the change to 200 variables. Hope that make sense

andreas_lds
Jade | Level 19

@BartlomiejR wrote:

Im not after the changing statement but just want to know how can apply the change to 200 variables. Hope that make sense


No, it does not make any sense to import 200 variables as numeric if you need them to be char-variables.

You can use two arrays (one for the char variable another for the old numerics) and a loop to process them.  As i said: this has been asked and answered many times, please use the search-box.

BartlomiejR
Calcite | Level 5

Maybe my question was not clear enough, sorry.Not after numeric to character change or old to new. I tried to find what I need in serch-box. but couldnt. Just after the proces to loop them. Thanks for replying

Kurt_Bremser
Super User

Transpose, change the resulting single variable to character, and transpose back:

data have;
input id $ q1 q2 q3;
datalines;
a 1 2 3
;
run;

proc transpose data=have out=trans;
by id;
var q:;
run;

data trans_new;
set trans;
length _col $1;
_col = put(col1,1.);
run;

proc transpose data=trans_new out=want (drop=_name_);
by id;
var _col;
id _name_;
run;
Amir
PROC Star

Welcome to the SAS forum. As you have suggested, it is not always easy to explain what is required in a question. Providing examples of before and after data is a big help, in the form of a data step as demonstrated at the beginning of @Kurt_Bremser's first response "data have".

 

With examples of have and want data sets, you can explain the rules / logic of how the have data becomes the want data.

 

Also check if the subject line is not a cause for confusion.

 

Amir.

Tom
Super User Tom
Super User

You need to use ARRAY statement to allow you to perform the same operation on many variables.

Assuming they are all using the same code list then just use a PUT() function with the format to make the value to text conversion.

data want;
  set have ;
  array old Q1-Q2000;
  array new $10 QX1-QX2000;
  do i=1 to dim(old);
    new=put(old(i),yesno.);
  end;
run;

If they different formats then use the VVALUE() function instead to get the formatted value of the variable based on whatever format the variable already has attached.  You might need to use the LEFT() function also to remove leading spaces in the formatted value, depending on how you want your character variables created.

 

As @Kurt_Bremser sasys you also might want to switch to a vertical format:

 

data tall;
  set have ;
  array old Q1-Q2000;
  length qnum 8 qname $32 code 8 decode $32 ;
  do qnum=1 to dim(old);
    qname=vname(old(qnum));
    code=old(qnum);
    decode=vvaluex(old(qnum));
    output;
  end;
  drop q1-q2000;
run;
ballardw
Super User

@BartlomiejR wrote:

Variables Q1-Q200 has numeric Values 1, 2, 3. Based on Questionnaire. 1=Yes, 

2=No, 3=Maybe. How would you assign to 200 new variables (QX1-QX200) the 

character equivalent of the 200 old variables without having to write 200+ lines of 

Code?


I wouldn't and don't. I use a FORMAT to display the values I want.

 

proc format library = work;
value ynm
1='Yes'
2='No'
3='Maybe'
;
run;

data example;
   input Q1-Q3;
datalines;
1 2 3
1 1 1
2 1 2
3 3 3
;
run;

proc print data=example;
   format Q1-Q3 ynm.;
run;

I think whoever designed your response scale did not do you any favors. I would tend to think that "Maybe" should be considered between "Yes" and "No" which would allow procedures that will work with ORDINAL values (order of value has some meaning).

 

I work with LOTS of survey data and almost never recode values in a one-to-one relationship but use formats to display desired text.

Some of my projects may involve many formats but it is much easier than recoding values. Also for specific questions if you want to treat two or more values as single category then just changing the format is much easier:

proc format library=work;
value ym
   1,3 = 'Yes/Maybe'
   2   = 'No'
;
run;

proc print data=example;
   format Q1 ynm. Q2 Q3 ym.;
run;

The groups created by the formats are respected by almost all of the analysis procedures where you would want to use the variable a category such as Proc Freq or a CLASS variable in Proc Means or other analysis procs

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 9 replies
  • 2468 views
  • 0 likes
  • 6 in conversation