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

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!

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20
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;

View solution in original post

4 REPLIES 4
Community_Guide
SAS Moderator

Hello @weweaw,


Your question requires more details before experts can help. Can you revise your question to include more information? 

 

Review this checklist:

  • Specify a meaningful subject line for your topic.  Avoid generic subjects like "need help," "SAS query," or "urgent."
  • When appropriate, provide sample data in text or DATA step format.  See this article for one method you can use.
  • If you're encountering an error in SAS, include the SAS log or a screenshot of the error condition. Use the Photos button to include the image in your message.
    use_buttons.png
  • It also helps to include an example (table or picture) of the result that you're trying to achieve.

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.

 

edit_post.png

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

.
novinosrin
Tourmaline | Level 20
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;
Astounding
PROC Star

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;

ballardw
Super User

@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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 4 replies
  • 758 views
  • 3 likes
  • 5 in conversation