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

I have a large number of variables for which I'd like to apply the same If then criteria:

 

	if A1 = 4 or A1 = 5 then
		A1_pos = 1;

	if A1 = 3 or A1 = 2 or A1 = 1 then
		A1_pos = 0;

	if A1 = . then
		A1_den = 0;
	else A1_den = 1;

	if A3 = 4 or A3 = 5 then
		A3_pos = 1;

	if A3 = 3 or A3 = 2 or A3 = 1 then
		A3_pos = 0;

	if A3 = . then
		A3_den = 0;
	else A3_den = 1;

 

Rather than write out a large number of if then statements, I'm thinking an array would could down on my code and be a more efficient way to do this.   Any suggestions are greatly appreciated!

 

Thank you

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

That is exactly what arrays are designed for.  From your code it looks like you have one input array and two output arrays.  Note that I would change the naming of the output variables to have the numeric part as the suffix since then it is easier to use variable name lists.

array VAL A1-A4;
array DEN DEN_A1-DEN_A4 ;
array POS POS_A1-POS_A4 ;

Now just wrap your logic inside of a DO loop and replace your variable references with array references.

do i=1 to dim(VAL);
  if val(i) in (4,5) then pos(i)=1;
  else if val(i) in (1,2,3) then pos(i)=0;

  if val(i)=. then den(i)=0
  else den(i)=1;

end;

View solution in original post

2 REPLIES 2
ballardw
Super User

Something like this should get you started. Change the 10 the largest number of like variables. Assumes that you do NOT have a variable named "a" in the data. If you do you will need a different name for the first array. Also assumes that the A variables are consecutively numbered, if that is not the case then you need to provide a list that does match. Note that you can mix sequences and single values such as Array a a1-a10 a15  a20-a26;

 

data want;
   set have;
   array a a1-a10;
   /* assumes the Pos and Den variables do not already exist. NOTE that your
   SAS code will run much smoother if you use the suffix as the number and not
   in the middle if the variables already exist you will need to list them out
   or possibly A1_Pos -- A10_Pos will work if they are in order in the data set
   (yes that is two dashes)*/ 
   array Pos_A {10};
   array Den_a {10};
   do i=1 to dim(a);
      if a[i] in (4,5) then Pos_A[i]=1;
      else if a[i] in (3,2,1) then Pos_A[i]=0;
      Den_A[i]= not missing(A[i]);
   end;
run;
Tom
Super User Tom
Super User

That is exactly what arrays are designed for.  From your code it looks like you have one input array and two output arrays.  Note that I would change the naming of the output variables to have the numeric part as the suffix since then it is easier to use variable name lists.

array VAL A1-A4;
array DEN DEN_A1-DEN_A4 ;
array POS POS_A1-POS_A4 ;

Now just wrap your logic inside of a DO loop and replace your variable references with array references.

do i=1 to dim(VAL);
  if val(i) in (4,5) then pos(i)=1;
  else if val(i) in (1,2,3) then pos(i)=0;

  if val(i)=. then den(i)=0
  else den(i)=1;

end;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 9827 views
  • 0 likes
  • 3 in conversation