How to use Arrays in SAS

Array: An array is a collection of similar type of data.

Demo Program 1: This program will create no change in the output.

data new;
   infile cards;
   input My_height My_weight My_age;
   cards;
   9 65  54
   5 9   78
   8 47  9
   ;
   if My_Height = 9 then My_Height = .;
   if My_Weight = 9 then My_Weight = .;
   if My_Age    = 9 then My_Age    = .;
run;
proc print;
run;

 

Demo Program 2: This program will create change in the output. Now you will see the DOT (.) instead of 9

data new;
   infile cards;
   input My_height My_weight My_age;
   if My_Height = 9 then My_Height = .;
   if My_Weight = 9 then My_Weight = .;
   if My_Age    = 9 then My_Age    = .;
cards;
   9 65  54
   5 9   78
   8 47  9
   ;
run;
proc print;
run;

 

Demo Program 3: Here I have used the DO loop

data new1;
   infile cards;
   input My_height My_weight My_age;
   cards;
   9 65  54
   5 9   78
   8 47  9
   ;
run;
data new1;
set new;
array myvars{3} My_height My_weight My_age;
do i= 1 to 3;
if myvars{i} = 9 then myvars{i}= .;
end;
drop i;
run;

 

Demo Program 4: This Program Converts the values of NV and ! to a character missing values.

data chars ;
   input A $ B $ x y Ques $;
datalines;
NV ! 3 4 ABC
AAA BBB 8 . !
NV NV 9 8 NV
;
run;
data missing;
      set chars;
      array my_chars{*} $ _Character_;
      do i= 1 to dim(my_chars);
      if my_chars{i} in ('NV' '!') then call missing(my_chars{i});
      end;
      drop i;
      run; 

 

Demo Program 5: It will convert all the values into LOWERCASE.

data chars;
input A $ B $ x y Ques $;
datalines;
NV ! 3 4 ABC
AAA BBB 8 . !
NV NV 9 8 NV
;
run;
data missing;
      set chars;
      array my_chars{*} $ _Character_;
      do i= 1 to dim(my_chars);
my_chars(i)= lowcase(my_Chars{i});
      end;
      drop i;
      run;
      proc print;
      run;

 

Demo Program 6: Here using DO loop we will first print the Fahren values from 1-24. And then Celsius values.

Celsius{Hour} = (Fahren{Hour} – 32)/1.8;

The above formula is used to convert the Fahren values to Celsius. Here I have used the double trailing (@@)

data temp;
   input Fahren1-Fahren24 @@;
   array Fahren[24];
   array Celsius[24] Celsius1-Celsius24;
   do Hour = 1 to 24;
      Celsius{Hour} = (Fahren{Hour} - 32)/1.8;
   end;
   drop Hour;
datalines;
3 7 10 2 4 58 51 58 69 63 54 66 88 20 32 45 65
82 96 25 63 92 90 41
;
run;
proc print;
run;

Demo Program 7:

data new;
input id value1-value12;
put "This is to use PDV and check the execution in the Log Window:"_all_;
cards;
1 1 2 3 4 5 6 7 8 9 10 11 12
2 21 22 23 24 25 26 27 28 29 30 31 32
;
run;
proc print data=new;
run;
data new1;
set new;
      new_value1=value1 * .01;
      new_value2=value2 * .01;
      new_value3=value3 * .01;
      new_value4=value4 * .01;
      new_value5=value5 * .01;
      new_value6=value6 * .01;
      new_value7=value7 * .01;
      new_value8=value8 * .01;
      new_value9=value9 * .01;
      new_value10=value10 * .01;
      new_value11=value11 * .01;
      new_value12=value12 * .01;
put "This is to use PDV and check the execution in the Log Window:"_all_;
run;
proc print data=new1;
run;


data new;
set new1;
      array value(12) value1-value12;
      array new_value(12) new_value1-new_value12;
            Do number= 1 to 12;
            value(number)=new_value(number) - 100000;
      end;
      drop number;
      put "This is to use PDV and check the execution in the Log Window:"_all_;
run;
proc print data=new2;
var value1-value12 new_value1-new_value12;
run;

Comments

comments

InRaMS

InRaMS technologies is a web based company here we have Digital marketing, web development. In digital marketing we have Small SEO tools and also Try this Website SEO Audit Tool

Leave a Reply

Your email address will not be published. Required fields are marked *

6 − 5 =

Time limit is exhausted. Please reload CAPTCHA.