Lesson 13, Part 1: Simulating Data Using PROC IML¶

In [3]:
/* https://communities.sas.com/t5/SAS-Programming/Convert-Full-State-Name-to-Abbreviation/td-p/738723;*/
DM "Log; clear; output; clear; odsresults; clear";
data fips;
  length fips 8 state_postal_code $2 state_name_territory $20 ;
  do fips=1 to 95;
    state_name_territory = fipnamel(fips); /* FIPNAMEL function converts the FIPS code 
	                                          to the corresponding state or U.S. territory name in mixed case" */
    if state_name_territory ne 'Invalid Code' then do;
        state_postal_code = fipstate(fips); /* FIPSTATE converts two-digit FIPS codes to two-character state postal codes */
	    state_name=stnamel(state_postal_code); /* STNAME function converts a two-character state postal code to the 
		                                        corresponding state name in mixed casease */
     output;
    end;
  end;
run;
proc print data=fips;
run;
SAS Output

The SAS System

Obs fips state_postal_code state_name_territory state_name
1 1 AL Alabama Alabama
2 2 AK Alaska Alaska
3 4 AZ Arizona Arizona
4 5 AR Arkansas Arkansas
5 6 CA California California
6 8 CO Colorado Colorado
7 9 CT Connecticut Connecticut
8 10 DE Delaware Delaware
9 11 DC District of Columbia District of Columbia
10 12 FL Florida Florida
11 13 GA Georgia Georgia
12 15 HI Hawaii Hawaii
13 16 ID Idaho Idaho
14 17 IL Illinois Illinois
15 18 IN Indiana Indiana
16 19 IA Iowa Iowa
17 20 KS Kansas Kansas
18 21 KY Kentucky Kentucky
19 22 LA Louisiana Louisiana
20 23 ME Maine Maine
21 24 MD Maryland Maryland
22 25 MA Massachusetts Massachusetts
23 26 MI Michigan Michigan
24 27 MN Minnesota Minnesota
25 28 MS Mississippi Mississippi
26 29 MO Missouri Missouri
27 30 MT Montana Montana
28 31 NE Nebraska Nebraska
29 32 NV Nevada Nevada
30 33 NH New Hampshire New Hampshire
31 34 NJ New Jersey New Jersey
32 35 NM New Mexico New Mexico
33 36 NY New York New York
34 37 NC North Carolina North Carolina
35 38 ND North Dakota North Dakota
36 39 OH Ohio Ohio
37 40 OK Oklahoma Oklahoma
38 41 OR Oregon Oregon
39 42 PA Pennsylvania Pennsylvania
40 44 RI Rhode Island Rhode Island
41 45 SC South Carolina South Carolina
42 46 SD South Dakota South Dakota
43 47 TN Tennessee Tennessee
44 48 TX Texas Texas
45 49 UT Utah Utah
46 50 VT Vermont Vermont
47 51 VA Virginia Virginia
48 53 WA Washington Washington
49 54 WV West Virginia West Virginia
50 55 WI Wisconsin Wisconsin
51 56 WY Wyoming Wyoming
52 60 AS American Samoa American Samoa
53 61 PQ Canal Zone Canal Zone
54 62 EQ Canton/Enderbury Is Canton/Enderbury Is
55 64 FM Fed State Micronesia Fed State Micronesia
56 66 GU Guam Guam
57 67 JQ Johnston Atoll Johnston Atoll
58 68 MH Marshall Islands Marshall Islands
59 69 MP Northern Mariana Isl Northern Mariana Isl
60 70 PW Palau Palau
61 71 MQ Midway Island Midway Island
62 72 PR Puerto Rico Puerto Rico
63 74 UM US Minor Outlying Is US Minor Outlying Is
64 75 TQ Trust Territories Pa Trust Territories Pa
65 76 BQ US Misc Carib Isl US Misc Carib Isl
66 77 IQ Navassa Island Navassa Island
67 78 VI Virgin Islands Virgin Islands
68 79 WQ Wake Island Wake Island
69 81 -- Baker Island  
70 84 -- Howland Island  
71 86 -- Jarvis Island  
72 89 -- Kingman Reef  
73 95 -- Palmyra Atoll  
In [5]:
*Ex4_generate_dates_sashelp_class.sas;
title;
data class ;
call streaminit(123);
    set sashelp.class;
      dob=Today()
          -floorz(age*365 + 
                  + floorz(100*rand("Uniform"))
                  +(rand("uniform"))
                  );
	  age2=int((today() - dob)/365.25);
	  /*use the INTCK function to measure the number of intervals 
      between two dates [ DOB and TODAY0 */
	  age3 = INTCK('YEAR',DOB,TODAY());
	  today_date=today();
	  age4=YRDIF(DoB, today(), 'ACT/ACT') ;
     format today_date dob date9.;
  run;
proc print data=class; run;
SAS Output
Obs Name Sex Age Height Weight dob age2 age3 today_date age4
1 Alfred M 14 69.0 112.5 06MAY2012 14 14 30JUN2026 14.1489
2 Alice F 13 56.5 84.0 26JUN2013 13 13 30JUN2026 13.0110
3 Barbara F 13 65.3 98.0 01JUN2013 13 13 30JUN2026 13.0795
4 Carol F 14 62.8 102.5 31MAY2012 14 14 30JUN2026 14.0806
5 Henry M 14 63.5 102.5 28JUN2012 14 14 30JUN2026 14.0041
6 James M 12 57.3 83.0 02APR2014 12 12 30JUN2026 12.2438
7 Jane F 12 59.8 84.5 17MAY2014 12 12 30JUN2026 12.1205
8 Janet F 15 62.5 112.5 20APR2011 15 15 30JUN2026 15.1945
9 Jeffrey M 13 62.5 84.0 31MAY2013 13 13 30JUN2026 13.0822
10 John M 12 59.0 99.5 02JUL2014 11 12 30JUN2026 11.9945
11 Joyce F 11 51.3 50.5 03APR2015 11 11 30JUN2026 11.2411
12 Judy F 14 64.3 90.0 26JUN2012 14 14 30JUN2026 14.0095
13 Louise F 12 56.3 77.0 23APR2014 12 12 30JUN2026 12.1863
14 Mary F 15 66.5 112.0 03APR2011 15 15 30JUN2026 15.2411
15 Philip M 16 72.0 150.0 13MAY2010 16 16 30JUN2026 16.1315
16 Robert M 12 64.8 128.0 19MAY2014 12 12 30JUN2026 12.1151
17 Ronald M 15 67.0 133.0 22MAY2011 15 15 30JUN2026 15.1068
18 Thomas M 11 57.5 85.0 02MAY2015 11 11 30JUN2026 11.1616
19 William M 15 66.5 112.0 07MAY2011 15 15 30JUN2026 15.1479
In [7]:
*Ex6_random_assignment.sas;
data dp;
CALL STREAMINIT(12345);
LENGTH Group $ 7;
 set sashelp.class;
  random_num = RAND("UNIFORM");
  if random_num >0.5 then Group='Drug';
  else group='Placebo';
run;
proc print data=dp noobs; 
 var Name Sex Age Height Weight random_num group;
run;
SAS Output
Name Sex Age Height Weight random_num Group
Alfred M 14 69.0 112.5 0.58330 Drug
Alice F 13 56.5 84.0 0.99363 Drug
Barbara F 13 65.3 98.0 0.58789 Drug
Carol F 14 62.8 102.5 0.85747 Drug
Henry M 14 63.5 102.5 0.82469 Drug
James M 12 57.3 83.0 0.28057 Placebo
Jane F 12 59.8 84.5 0.64740 Drug
Janet F 15 62.5 112.5 0.38192 Placebo
Jeffrey M 13 62.5 84.0 0.44896 Placebo
John M 12 59.0 99.5 0.87578 Drug
Joyce F 11 51.3 50.5 0.51838 Drug
Judy F 14 64.3 90.0 0.84267 Drug
Louise F 12 56.3 77.0 0.27838 Placebo
Mary F 15 66.5 112.0 0.93354 Drug
Philip M 16 72.0 150.0 0.18739 Placebo
Robert M 12 64.8 128.0 0.36876 Placebo
Ronald M 15 67.0 133.0 0.10107 Placebo
Thomas M 11 57.5 85.0 0.74119 Drug
William M 15 66.5 112.0 0.15068 Placebo
In [13]:
* Ex9_simulate.sas;
%LET Path=C:\Explore\SAS\Lesson13\Lesson13data;
LIBNAME NEW "&Path";
%MACRO generate_state(state=, nstores=);
   data _null_;
     CALL STREAMINIT(1234);
      file "&PATH/&state..txt";
      %DO i = 1 %TO &nstores;
	     sales = FLOORZ(80000 + RAND("NORMAL") * 15000);
            put sales 7.;
            output;
	      %END;
	  run;
%MEND generate_state;
Filename mprint "&path\California.sas";
options mprint mfile;
%generate_state(state = California, nstores = 8)

Filename mprint "&path\Texas.sas";
options mprint mfile;
%generate_state(state = Texas,  nstores = 5)

%MACRO generate_state(state=, nstores=);
data _null_;
 CALL STREAMINIT(1234);
 file "&PATH/&state..txt";
        do x=1 to &nstores;
		    sales = FLOORZ(80000 + RAND("NORMAL") * 15000);
            put sales 7.;
           output;
        end;
        run;
%MEND generate_state;
%generate_state(state = California_x, nstores = 8)
%generate_state(state = Texas_x, nstores = 5)
29                                                         The SAS System                               23:53 Tuesday, June 30, 2026

627        ods listing close;ods html5 (id=saspy_internal) file=_tomods1 options(bitmap_mode='inline') device=svg style=HTMLBlue;
627      ! ods graphics on / outputfmt=png;
NOTE: Writing HTML5(SASPY_INTERNAL) Body file: _TOMODS1
628        
629        * Ex9_simulate.sas;
630        %LET Path=C:\Explore\SAS\Lesson13\Lesson13data;
631        LIBNAME NEW "&Path";
NOTE: Libref NEW was successfully assigned as follows: 
      Engine:        V9 
      Physical Name: C:\Explore\SAS\Lesson13\Lesson13data
632        %MACRO generate_state(state=, nstores=);
633           data _null_;
634             CALL STREAMINIT(1234);
635              file "&PATH/&state..txt";
636              %DO i = 1 %TO &nstores;
637        	     sales = FLOORZ(80000 + RAND("NORMAL") * 15000);
638                    put sales 7.;
639                    output;
640        	      %END;
641        	  run;
642        %MEND generate_state;
643        Filename mprint "&path\California.sas";
644        options mprint mfile;
645        %generate_state(state = California, nstores = 8)
MPRINT(GENERATE_STATE):   data _null_;
NOTE: The macro generated output from MPRINT will also be written to external file 
      C:\Explore\SAS\Lesson13\Lesson13data\California.sas while OPTIONS MPRINT and MFILE are set.
MPRINT(GENERATE_STATE):   CALL STREAMINIT(1234);
MPRINT(GENERATE_STATE):   file "C:\Explore\SAS\Lesson13\Lesson13data/California.txt";
MPRINT(GENERATE_STATE):   sales = FLOORZ(80000 + RAND("NORMAL") * 15000);
MPRINT(GENERATE_STATE):   put sales 7.;
MPRINT(GENERATE_STATE):   output;
MPRINT(GENERATE_STATE):   sales = FLOORZ(80000 + RAND("NORMAL") * 15000);
MPRINT(GENERATE_STATE):   put sales 7.;
MPRINT(GENERATE_STATE):   output;
MPRINT(GENERATE_STATE):   sales = FLOORZ(80000 + RAND("NORMAL") * 15000);
MPRINT(GENERATE_STATE):   put sales 7.;
MPRINT(GENERATE_STATE):   output;
MPRINT(GENERATE_STATE):   sales = FLOORZ(80000 + RAND("NORMAL") * 15000);
MPRINT(GENERATE_STATE):   put sales 7.;
MPRINT(GENERATE_STATE):   output;
MPRINT(GENERATE_STATE):   sales = FLOORZ(80000 + RAND("NORMAL") * 15000);
MPRINT(GENERATE_STATE):   put sales 7.;
MPRINT(GENERATE_STATE):   output;
MPRINT(GENERATE_STATE):   sales = FLOORZ(80000 + RAND("NORMAL") * 15000);
MPRINT(GENERATE_STATE):   put sales 7.;
MPRINT(GENERATE_STATE):   output;
MPRINT(GENERATE_STATE):   sales = FLOORZ(80000 + RAND("NORMAL") * 15000);
MPRINT(GENERATE_STATE):   put sales 7.;
MPRINT(GENERATE_STATE):   output;
MPRINT(GENERATE_STATE):   sales = FLOORZ(80000 + RAND("NORMAL") * 15000);
MPRINT(GENERATE_STATE):   put sales 7.;
MPRINT(GENERATE_STATE):   output;
MPRINT(GENERATE_STATE):   run;

NOTE: The file "C:\Explore\SAS\Lesson13\Lesson13data/California.txt" is:
      Filename=C:\Explore\SAS\Lesson13\Lesson13data\California.txt,
      RECFM=V,LRECL=32767,File Size (bytes)=0,
      Last Modified=01Jul2026:00:01:02,
      Create Time=01Jul2026:00:01:02

NOTE: 8 records were written to the file "C:\Explore\SAS\Lesson13\Lesson13data/California.txt".
      The minimum record length was 7.
      The maximum record length was 7.
NOTE: DATA statement used (Total process time):
      real time           0.06 seconds
      cpu time            0.03 seconds
      

646        
647        Filename mprint "&path\Texas.sas";
648        options mprint mfile;
649        %generate_state(state = Texas,  nstores = 5)
MPRINT(GENERATE_STATE):   data _null_;
NOTE: The macro generated output from MPRINT will also be written to external file C:\Explore\SAS\Lesson13\Lesson13data\Texas.sas 
      while OPTIONS MPRINT and MFILE are set.
MPRINT(GENERATE_STATE):   CALL STREAMINIT(1234);
MPRINT(GENERATE_STATE):   file "C:\Explore\SAS\Lesson13\Lesson13data/Texas.txt";
MPRINT(GENERATE_STATE):   sales = FLOORZ(80000 + RAND("NORMAL") * 15000);
MPRINT(GENERATE_STATE):   put sales 7.;
MPRINT(GENERATE_STATE):   output;
MPRINT(GENERATE_STATE):   sales = FLOORZ(80000 + RAND("NORMAL") * 15000);
MPRINT(GENERATE_STATE):   put sales 7.;
MPRINT(GENERATE_STATE):   output;
MPRINT(GENERATE_STATE):   sales = FLOORZ(80000 + RAND("NORMAL") * 15000);
MPRINT(GENERATE_STATE):   put sales 7.;
MPRINT(GENERATE_STATE):   output;
MPRINT(GENERATE_STATE):   sales = FLOORZ(80000 + RAND("NORMAL") * 15000);
MPRINT(GENERATE_STATE):   put sales 7.;
MPRINT(GENERATE_STATE):   output;
MPRINT(GENERATE_STATE):   sales = FLOORZ(80000 + RAND("NORMAL") * 15000);
MPRINT(GENERATE_STATE):   put sales 7.;
MPRINT(GENERATE_STATE):   output;
MPRINT(GENERATE_STATE):   run;

NOTE: The file "C:\Explore\SAS\Lesson13\Lesson13data/Texas.txt" is:
      Filename=C:\Explore\SAS\Lesson13\Lesson13data\Texas.txt,
      RECFM=V,LRECL=32767,File Size (bytes)=0,
      Last Modified=01Jul2026:00:01:02,
      Create Time=01Jul2026:00:01:02

NOTE: 5 records were written to the file "C:\Explore\SAS\Lesson13\Lesson13data/Texas.txt".
      The minimum record length was 7.
      The maximum record length was 7.
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.01 seconds
      

650        
651        %MACRO generate_state(state=, nstores=);
652        data _null_;
653         CALL STREAMINIT(1234);
654         file "&PATH/&state..txt";
655                do x=1 to &nstores;
656        		    sales = FLOORZ(80000 + RAND("NORMAL") * 15000);
657                    put sales 7.;
658                   output;
659                end;
660                run;
661        %MEND generate_state;
662        %generate_state(state = California_x, nstores = 8)
MPRINT(GENERATE_STATE):   data _null_;
MPRINT(GENERATE_STATE):   CALL STREAMINIT(1234);
MPRINT(GENERATE_STATE):   file "C:\Explore\SAS\Lesson13\Lesson13data/California_x.txt";
MPRINT(GENERATE_STATE):   do x=1 to 8;
MPRINT(GENERATE_STATE):   sales = FLOORZ(80000 + RAND("NORMAL") * 15000);
MPRINT(GENERATE_STATE):   put sales 7.;
MPRINT(GENERATE_STATE):   output;
MPRINT(GENERATE_STATE):   end;
MPRINT(GENERATE_STATE):   run;

NOTE: The file "C:\Explore\SAS\Lesson13\Lesson13data/California_x.txt" is:
      Filename=C:\Explore\SAS\Lesson13\Lesson13data\California_x.txt,
      RECFM=V,LRECL=32767,File Size (bytes)=0,
      Last Modified=01Jul2026:00:01:02,
      Create Time=01Jul2026:00:01:02

NOTE: 8 records were written to the file "C:\Explore\SAS\Lesson13\Lesson13data/California_x.txt".
      The minimum record length was 7.
      The maximum record length was 7.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds
      

663        %generate_state(state = Texas_x, nstores = 5)
MPRINT(GENERATE_STATE):   data _null_;
MPRINT(GENERATE_STATE):   CALL STREAMINIT(1234);
MPRINT(GENERATE_STATE):   file "C:\Explore\SAS\Lesson13\Lesson13data/Texas_x.txt";
MPRINT(GENERATE_STATE):   do x=1 to 5;
MPRINT(GENERATE_STATE):   sales = FLOORZ(80000 + RAND("NORMAL") * 15000);
MPRINT(GENERATE_STATE):   put sales 7.;
MPRINT(GENERATE_STATE):   output;
MPRINT(GENERATE_STATE):   end;
MPRINT(GENERATE_STATE):   run;

NOTE: The file "C:\Explore\SAS\Lesson13\Lesson13data/Texas_x.txt" is:
      Filename=C:\Explore\SAS\Lesson13\Lesson13data\Texas_x.txt,
      RECFM=V,LRECL=32767,File Size (bytes)=0,
      Last Modified=01Jul2026:00:01:02,
      Create Time=01Jul2026:00:01:02

NOTE: 5 records were written to the file "C:\Explore\SAS\Lesson13\Lesson13data/Texas_x.txt".
      The minimum record length was 7.
      The maximum record length was 7.
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.00 seconds
      

664        
665        
666        
667        ods html5 (id=saspy_internal) close;ods listing;
668        
30                                                         The SAS System                               23:53 Tuesday, June 30, 2026

669        
In [ ]: