DATA Step, Macro, Functions and more

If in do loop or macro in data step

Accepted Solution Solved
Reply
Contributor
Posts: 21
Accepted Solution

If in do loop or macro in data step

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!


Accepted Solutions
Solution
2 weeks ago
PROC Star
Posts: 1,845

Re: If in do loop or macro in data step

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


All Replies
SAS Moderator
Posts: 62

Re: If in do loop or macro in data step [how to improve your question]

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

.
Solution
2 weeks ago
PROC Star
Posts: 1,845

Re: If in do loop or macro in data step

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;
Super User
Posts: 6,785

Re: If in do loop or macro in data step

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;

Super User
Posts: 13,583

Re: If in do loop or macro in data step


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

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 72 views
  • 3 likes
  • 5 in conversation