BookmarkSubscribeRSS Feed
Ronein
Meteorite | Level 14

Hello

In following row data for each customer there is information about number of months that he was exposed to illness. 

What is the way to write this code in a shorter and more useful way.

Also in the real data maximum number of months of exposition is 18 (and not 6).

I would like to know how to write a more general code that work also for 18 months.

 


Data Rawdata;
Input ID No_Mon_expose;
cards;
1 6
2 3
3 3
4 4
5 2
6 1
7 6
8 3
9 2
10 6
;
run;


Data wanted;
set Rawdata;
if No_Mon_expose=6 then do;
Ind_Expose_Mon1=1;
Ind_Expose_Mon2=1;
Ind_Expose_Mon3=1;
Ind_Expose_Mon4=1;
Ind_Expose_Mon5=1;
Ind_Expose_Mon6=1;
end;
else if No_Mon_expose=5 then do;
Ind_Expose_Mon1=1;
Ind_Expose_Mon2=1;
Ind_Expose_Mon3=1;
Ind_Expose_Mon4=1;
Ind_Expose_Mon5=1;
Ind_Expose_Mon6=0;
end;
else if No_Mon_expose=4 then do;
Ind_Expose_Mon1=1;
Ind_Expose_Mon2=1;
Ind_Expose_Mon3=1;
Ind_Expose_Mon4=1;
Ind_Expose_Mon5=0;
Ind_Expose_Mon6=0;
end;
else if No_Mon_expose=3 then do;
Ind_Expose_Mon1=1;
Ind_Expose_Mon2=1;
Ind_Expose_Mon3=1;
Ind_Expose_Mon4=0;
Ind_Expose_Mon5=0;
Ind_Expose_Mon6=0;
end;
else if No_Mon_expose=2 then do;
Ind_Expose_Mon1=1;
Ind_Expose_Mon2=1;
Ind_Expose_Mon3=0;
Ind_Expose_Mon4=0;
Ind_Expose_Mon5=0;
Ind_Expose_Mon6=0;
end;
else if No_Mon_expose=1 then do;
Ind_Expose_Mon1=1;
Ind_Expose_Mon2=0;
Ind_Expose_Mon3=0;
Ind_Expose_Mon4=0;
Ind_Expose_Mon5=0;
Ind_Expose_Mon6=0;
end;
Run;

 

4 REPLIES 4
PeterClemmensen
Tourmaline | Level 20

one way

 

data want;
    set Rawdata;
    array Ind_Expose_Mon {18} (18*0);
    do _N_=1 to No_Mon_expose;
        Ind_Expose_Mon [_N_] = 1;
    end;
run;
ed_sas_member
Meteorite | Level 14

Hi @Ronein 

 

Here is a way to achieve this using an array:

data Rawdata_long;
	set Rawdata;
	
	array Ind_Expose_Mon (6); /* replace by 18 */

	do i=1 to dim(Ind_Expose_Mon);
		if i<=No_Mon_expose then Ind_Expose_Mon(i)=1;
		else Ind_Expose_Mon(i)=0;
	end;
	
	drop i;
run;
ballardw
Super User

very likely making a wide data set will make later steps harder, depending on how this data is to be used.

Tom
Super User Tom
Super User

You should be able to do this with an array.  Take advantage of the fact that SAS evaluate boolean expressions to 0 or 1 to make setting the value easier.

data wanted;
  set Rawdata;
  array Ind_Expose_Mon [18];
  do index=1 to dim(Ind_Expose_Mon);
    Ind_Expose_Mon[index]= (index <= No_Mon_expose) ;
  end;
  drop index;
run;

Whether you want to or not is a different question.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 377 views
  • 1 like
  • 5 in conversation