Hello,
I want to make indicator variables if the second two digits are a particular set. The variable its self it char. The numbers go from 0-9 for both positions. If it was h678 then I want to make an indicator var to be called _67 (or nit67) and that to be 1. I've been trying to make a do loop but cannot make it work.
This is what I have:
id | nit |
234 | a019 |
453 | b033 |
457 | c101 |
And this is what I want:
id | nit | _01 | _02 | _03 | _04 | _05 | _06 | _07 | _08 | _09 | _10 |
234 | a019 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
453 | b033 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
457 | c101 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 |
Thank you!
data have;
input id nit $;
cards;
234 a019
453 b033
457 c101
;
data want;
set have;
array t(*) _01-_10;
do _n_=1 to dim(t);
t(_n_)= substr(vname(t(_n_)),2,2)=substr(nit,2,2) ;
end;
run;
Hello @weweaw,
Your question requires more details before experts can help. Can you revise your question to include more information?
Review this checklist:
To edit your original message, select the "blue gear" icon at the top of the message and select Edit Message. From there you can adjust the title and add more details to the body of the message. Or, simply reply to this message with any additional information you can supply.
SAS experts are eager to help -- help them by providing as much detail as you can.
This prewritten response was triggered for you by fellow SAS Support Communities member @Reeza
.data have;
input id nit $;
cards;
234 a019
453 b033
457 c101
;
data want;
set have;
array t(*) _01-_10;
do _n_=1 to dim(t);
t(_n_)= substr(vname(t(_n_)),2,2)=substr(nit,2,2) ;
end;
run;
It's a little easier if you are willing to accept missing values instead of zeros. But this should get exactly what you asked for:
data want;
set have;
retain nit01 - nit99 0;
array nitflags {99} nit01 - nit99;
flagnum = input(substr(nit, 2), 2.);
if (1 <= flagnum <= 99) then do;
nitflags{flagnum} = 1;
output;
nitflags{flagnum} = 0;
end;
else output;
drop flagnum;
run;
@weweaw wrote:
Hello,
I want to make indicator variables if the second two digits are a particular set. The variable its self it char. The numbers go from 0-9 for both positions. If it was h678 then I want to make an indicator var to be called _67 (or nit67) and that to be 1. I've been trying to make a do loop but cannot make it work.
This is what I have:
id nit 234 a019 453 b033 457 c101 And this is what I want:
id nit _01 _02 _03 _04 _05 _06 _07 _08 _09 _10 234 a019 1 0 0 0 0 0 0 0 0 0 453 b033 0 0 1 0 0 0 0 0 0 0 457 c101 0 0 0 0 0 0 0 0 0 1
Thank you!
I think you want something similar to:
data want; set have; array _ _00-_99; test=input(substr(nit,2,2),best.); do i=1 to dim(_); _[i]= (test+1=i); end; drop i; run;
Your "description" may have missed that you need to have an _00 indicator. If you intended 00 to be _100 you need to state so.
Test is the numeric value of the second and third characters. If that is not a valid number you will get 0 for all indicators.
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 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.