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

 

 

I am relatively new to SAS, although I am learning. My only prior programming experience was with Tradestation Easy Language which, is, way more intuitive than SAS. Here, I am looking to run several statements at once. In Tradestation, it would run something like this:

 

If Condition 1 then begin ;

Variable1 = X

Variable2 = Y

Variable3 = Z ;

...Variable1000 = ZZ ;   (ETC.)

End ;

 

Ok... now in SAS, I want to do something similar. I have looked at arrays, and maybe that is the best way here, or not.

Below is the code. Now, if I had 100 different arguments or variables etc, which would be calculated if condition 1 were true, what is the most efficient way to write this code? Here, the condition is Nmiss(of Score1-Score5) eq 0. How can I only write this condition once and have the Maxscore, SecondLargest, and Minscore calculated?

 

Data review.Evaluate ; 
	Set learn.psych ; 
	If Nmiss(of Score1-Score5) eq 0 then Maxscore = Max(of Score1-score5) ; 
	If Nmiss(of Score1-Score5) eq 0 then SecondLargest = Largest(2, of Score1-score5) ;
	If Nmiss(of Score1-Score5) eq 0 then Minscore = Min(of Score1-Score5) ; 
run ; 

 

1 ACCEPTED SOLUTION

Accepted Solutions
HB
Barite | Level 11 HB
Barite | Level 11

 

 

 

http://documentation.sas.com/?docsetId=basess&docsetTarget=p0pcj5ajwyngron1wlsq0tet0hce.htm&docsetVe...

 

 

   /* a more efficient method */
data updatedattractions2;
   set mylib.attractions;
   if City = 'Madrid' then
      do;
         Museums = 3;
         Other = 2;
      end;

View solution in original post

3 REPLIES 3
HB
Barite | Level 11 HB
Barite | Level 11

 

 

 

http://documentation.sas.com/?docsetId=basess&docsetTarget=p0pcj5ajwyngron1wlsq0tet0hce.htm&docsetVe...

 

 

   /* a more efficient method */
data updatedattractions2;
   set mylib.attractions;
   if City = 'Madrid' then
      do;
         Museums = 3;
         Other = 2;
      end;
ManitobaMoose
Quartz | Level 8

Ok. So loop, without arrays, seems to be the most efficient in this case. Thanks.

ballardw
Super User

Mostly to have an answer for others in the future with a similar question an example based directly on the question code:

Data review.Evaluate ; 
   Set learn.psych ; 
   If Nmiss(of Score1-Score5) eq 0 then do;
      Maxscore = Max(of Score1-score5) ; 
      SecondLargest = Largest(2, of Score1-score5) ;
      Minscore = Min(of Score1-Score5) ; 
   end;
run ; 

Which is an example of "factoring out the common code", the Nmiss condition.

 

If you have different blocks of code based on how many were missing you could use the Select block such as:

Data review.Evaluate ; 
   Set learn.psych ; 
   select (Nmiss(of Score1-Score5));
      when (0) do; <some block of code>; end;
      when (1,2,3) do; <some other block of code>; end;
      when (4,5) do; <some different block of code>; end;
      otherwise do; <something or nothing>;end;
   end;

Which evaluates the Nmiss(of Score1-Score5) and branches to the appropriate number value indicated in the WHEN clauses within the ().

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
  • 3 replies
  • 797 views
  • 1 like
  • 3 in conversation