BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ChuksManuel
Pyrite | Level 9
if q10_7= '1' then q10_7a=0;
if q10_7= '2' then q10_7a=1;
if q10_7= '3' then q10_7a=2;
if q10_7= '4' then q10_7a=3;
if q10_7= '5' then q10_7a=4;

if q10_8= '1' then q10_8a=0;
if q10_8= '2' then q10_8a=1;
if q10_8= '3' then q10_8a=2;
if q10_8= '4' then q10_8a=3;
if q10_8= '5' then q10_8a=4;

if q10_9= '1' then q10_9a=0;
if q10_9= '2' then q10_9a=1;
if q10_9= '3' then q10_9a=2;
if q10_9= '4' then q10_9a=3;
if q10_9= '5' then q10_9a=4;

if q10_4= '1' then q10_4a=0;
if q10_4= '2' then q10_4a=1;
if q10_4= '3' then q10_4a=2;
if q10_4= '4' then q10_4a=3;
if q10_4= '5' then q10_4a=4;

if q10_5= '1' then q10_5a=0;
if q10_5= '2' then q10_5a=1;
if q10_5= '3' then q10_5a=2;
if q10_5= '4' then q10_5a=3;
if q10_5= '5' then q10_5a=4;

if q10_6= '1' then q10_6a=0;
if q10_6= '2' then q10_6a=1;
if q10_6= '3' then q10_6a=2;
if q10_6= '4' then q10_6a=3;
if q10_6= '5' then q10_6a=4;

Dear programmers, is there a way i can make these code easier?

I am just trying to switch numbers to another...eg. "1" in original dataset to "0", "2" to "1" etc. I know i can do an "if..then" statement but that seems long.

Can anyone give me an idea how i can do this?

 

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

One way:

 

q10_7a = input(q10_7, 1.) - 1;

q10_8a = input(q10_8, 1.) - 1;

You will need one statement for each group of statements.

This logic assumes that you want to do this for all possible values of the original variables, and they are all one digit long.

 

Depending on the number of questions that you need treated in this way, the code can be made shorter by using an array.  But for that to happen, we might want to change the names of the new variables.  Start here and see whether we're moving in the right direction.

View solution in original post

7 REPLIES 7
Astounding
PROC Star

One way:

 

q10_7a = input(q10_7, 1.) - 1;

q10_8a = input(q10_8, 1.) - 1;

You will need one statement for each group of statements.

This logic assumes that you want to do this for all possible values of the original variables, and they are all one digit long.

 

Depending on the number of questions that you need treated in this way, the code can be made shorter by using an array.  But for that to happen, we might want to change the names of the new variables.  Start here and see whether we're moving in the right direction.

ChuksManuel
Pyrite | Level 9
Thanks. Worked quite fine
Reeza
Super User

Here's a tutorial on using Arrays in SAS
https://stats.idre.ucla.edu/sas/seminars/sas-arrays/

 

 

You want to use arrays at minimum. I would also consider switching to an INFORMAT or FORMAT to recode the values rather than multiple IF/THEN but that's definitely an optional method. 

 

data want;
set have;
array qs(*) q10_7 q10_8 q10_9 q10_4  ; *list all questions you want to recode here;

do i=1 to dim(qs);
   if  qs(i) =  '1' then q10_9a=0;
   else if qs(i)  = '2' then q10_9a=1;
else if qs(i)  = '3' then q10_9a=2;
else if qs(i) = '4' then q10_9a=3;
else if qs(i) =  = '5' then q10_9a=4

end;

run;

@ChuksManuel wrote:
if q10_7= '1' then q10_7a=0;
if q10_7= '2' then q10_7a=1;
if q10_7= '3' then q10_7a=2;
if q10_7= '4' then q10_7a=3;
if q10_7= '5' then q10_7a=4;

if q10_8= '1' then q10_8a=0;
if q10_8= '2' then q10_8a=1;
if q10_8= '3' then q10_8a=2;
if q10_8= '4' then q10_8a=3;
if q10_8= '5' then q10_8a=4;

if q10_9= '1' then q10_9a=0;
if q10_9= '2' then q10_9a=1;
if q10_9= '3' then q10_9a=2;
if q10_9= '4' then q10_9a=3;
if q10_9= '5' then q10_9a=4;

if q10_4= '1' then q10_4a=0;
if q10_4= '2' then q10_4a=1;
if q10_4= '3' then q10_4a=2;
if q10_4= '4' then q10_4a=3;
if q10_4= '5' then q10_4a=4;

if q10_5= '1' then q10_5a=0;
if q10_5= '2' then q10_5a=1;
if q10_5= '3' then q10_5a=2;
if q10_5= '4' then q10_5a=3;
if q10_5= '5' then q10_5a=4;

if q10_6= '1' then q10_6a=0;
if q10_6= '2' then q10_6a=1;
if q10_6= '3' then q10_6a=2;
if q10_6= '4' then q10_6a=3;
if q10_6= '5' then q10_6a=4;

Dear programmers, is there a way i can make these code easier?

I am just trying to switch numbers to another...eg. "1" in original dataset to "0", "2" to "1" etc. I know i can do an "if..then" statement but that seems long.

Can anyone give me an idea how i can do this?

 

Thanks


 

ChrisNZ
Tourmaline | Level 20

Like this?

data _F;
  retain TYPE 'I' FMTNAME 'remap';
  do START='1','2','3','4','5';
    LABEL=input(START,1.)-1;
    output;
  end;
run;

proc format cntlin=_F; run;

data WANT;
  Q10_7 = '1' ;
  Q10_8 = '4' ;
  Q10_7A=input(Q10_7,remap.);
  Q10_8A=input(Q10_8,remap.);
run;

 

 

ChrisNZ
Tourmaline | Level 20

Arrays make the code harder to read. Only use them if you have a large number of variables.

Likewise, hard-code the proc format rather than use an input data set as I did, unless you have a large number of values to map.

Readability is always to be preferred. 

edasdfasdfasdfa
Quartz | Level 8

What would be considered a large number of variables?

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 7 replies
  • 739 views
  • 5 likes
  • 6 in conversation