Saturday, 30 August 2025

274.CYLINDER DATA MANAGEMENT AND ANALYTICS IN SAS: FROM CREATION TO INSIGHTS USING PROC PRINT | PROC FREQ | PROC MEANS | PROC SQL | DATA STEP | MACROS

CYLINDER DATA MANAGEMENT AND ANALYTICS IN SAS: FROM CREATION TO INSIGHTS USING  PROC PRINT | PROC FREQ | PROC MEANS | PROC SQL | DATA STEP | MACROS

 /*Creating A Dataset Of Cylinders */

Step 1: Create the Cylinder Dataset with >10 Observations 

data cylinders;

  length Cylinder_ID $4 Type $20 Material $10 Use $15 Location $15 Safety_Feature $20;

  input Cylinder_ID $ Type $ Capacity Material $ Use $ Location $ Safety_Feature $;

  datalines;

C001 LPG_Domestic 14.2 Steel Home Delhi Pressure_Valve

C002 LPG_Domestic 14.2 Steel Home Mumbai Pressure_Valve

C003 LPG_Commercial 19 Steel Restaurant Chennai Flame_Retardant

C004 LPG_Commercial 47.5 Steel Hotel Kolkata Pressure_Valve

C005 Mini_LPG 2 Composite Vendor Patna Translucent_Body

C006 Fiber_LPG 5 Composite Home Nagpur Pressure_Valve

C007 Fiber_LPG 10 Composite Hotel Kanpur Overpressure_Relief

C008 Industrial_Type1 50 Steel Factory Pune Overpressure_Relief

C009 Industrial_Type2 60 Aluminum Factory Surat Pressure_Valve

C010 Hydraulic_Single 45 Steel Construction Ahmedabad Overpressure_Relief

C011 Hydraulic_Double 60 Aluminum Machinery Bengaluru Pressure_Valve

C012 Telescopic 80 Steel DumpTruck Jaipur Overpressure_Relief

C013 TieRod 40 Steel Manufacturing Surat Pressure_Valve

C014 WeldedBody 65 Steel Manufacturing Hyderabad Flame_Retardant

C015 LOT_Cylinder 425 Steel Plant Gurgaon HDPE_Tube

;

run;

proc print;run;

Output:

ObsCylinder_IDTypeMaterialUseLocationSafety_FeatureCapacity
1C001LPG_DomesticSteelHomeDelhiPressure_Valve14.2
2C002LPG_DomesticSteelHomeMumbaiPressure_Valve14.2
3C003LPG_CommercialSteelRestaurantChennaiFlame_Retardant19.0
4C004LPG_CommercialSteelHotelKolkataPressure_Valve47.5
5C005Mini_LPGCompositeVendorPatnaTranslucent_Body2.0
6C006Fiber_LPGCompositeHomeNagpurPressure_Valve5.0
7C007Fiber_LPGCompositeHotelKanpurOverpressure_Relief10.0
8C008Industrial_Type1SteelFactoryPuneOverpressure_Relief50.0
9C009Industrial_Type2AluminumFactorySuratPressure_Valve60.0
10C010Hydraulic_SingleSteelConstructionAhmedabadOverpressure_Relief45.0
11C011Hydraulic_DoubleAluminumMachineryBengaluruPressure_Valve60.0
12C012TelescopicSteelDumpTruckJaipurOverpressure_Relief80.0
13C013TieRodSteelManufacturingSuratPressure_Valve40.0
14C014WeldedBodySteelManufacturingHyderabadFlame_Retardant65.0
15C015LOT_CylinderSteelPlantGurgaonHDPE_Tube425.0


Step 2: View the Data                        

proc print data=cylinders;

  title 'Cylinder Inventory (Sample from India)';

run;

Output:

Cylinder Inventory (Sample from India)

ObsCylinder_IDTypeMaterialUseLocationSafety_FeatureCapacity
1C001LPG_DomesticSteelHomeDelhiPressure_Valve14.2
2C002LPG_DomesticSteelHomeMumbaiPressure_Valve14.2
3C003LPG_CommercialSteelRestaurantChennaiFlame_Retardant19.0
4C004LPG_CommercialSteelHotelKolkataPressure_Valve47.5
5C005Mini_LPGCompositeVendorPatnaTranslucent_Body2.0
6C006Fiber_LPGCompositeHomeNagpurPressure_Valve5.0
7C007Fiber_LPGCompositeHotelKanpurOverpressure_Relief10.0
8C008Industrial_Type1SteelFactoryPuneOverpressure_Relief50.0
9C009Industrial_Type2AluminumFactorySuratPressure_Valve60.0
10C010Hydraulic_SingleSteelConstructionAhmedabadOverpressure_Relief45.0
11C011Hydraulic_DoubleAluminumMachineryBengaluruPressure_Valve60.0
12C012TelescopicSteelDumpTruckJaipurOverpressure_Relief80.0
13C013TieRodSteelManufacturingSuratPressure_Valve40.0
14C014WeldedBodySteelManufacturingHyderabadFlame_Retardant65.0
15C015LOT_CylinderSteelPlantGurgaonHDPE_Tube425.0

Step 3: Frequency of Cylinder Types       

proc freq data=cylinders;

  tables Type;

  title 'Frequency Table for Cylinder Types';

run;

Output:

Frequency Table for Cylinder Types

The FREQ Procedure

TypeFrequencyPercentCumulative
Frequency
Cumulative
Percent
Fiber_LPG213.33213.33
Hydraulic_Double16.67320.00
Hydraulic_Single16.67426.67
Industrial_Type116.67533.33
Industrial_Type216.67640.00
LOT_Cylinder16.67746.67
LPG_Commercial213.33960.00
LPG_Domestic213.331173.33
Mini_LPG16.671280.00
Telescopic16.671386.67
TieRod16.671493.33
WeldedBody16.6715100.00

Step 4: Summary Statistics for Capacity                   

proc means data=cylinders N MEAN MIN MAX;

  var Capacity;

  title 'Summary Statistics of Cylinder Capacity';

run;

Output:

Summary Statistics of Cylinder Capacity

The MEANS Procedure

Analysis Variable : Capacity
NMeanMinimumMaximum
1562.46000002.0000000425.0000000

Step 5: SQL Query to Count and Summarize by Location  

proc sql;

  create table Cylinder_Summary as

  select Location, count(*) as Num_Cylinders, avg(Capacity) as Avg_Capacity

  from cylinders

  group by Location;

quit;


proc print data=Cylinder_Summary;

  title 'Summary: Cylinders by Location';

run;

Output:

Summary: Cylinders by Location

ObsLocationNum_CylindersAvg_Capacity
1Ahmedabad145.0
2Bengaluru160.0
3Chennai119.0
4Delhi114.2
5Gurgaon1425.0
6Hyderabad165.0
7Jaipur180.0
8Kanpur110.0
9Kolkata147.5
10Mumbai114.2
11Nagpur15.0
12Patna12.0
13Pune150.0
14Surat250.0

Step 6: Create Macro for Region-specific Report           

%macro region_report(city);

  proc sql noprint;

    select count(*) into :num_cyl

    from cylinders

    where Location="&city";

  quit;

  %put Number of Cylinders in &city: &num_cyl;


  proc means data=cylinders noprint;

    var Capacity;

    where Location="&city";

    output out=stats mean=avg_capacity;

  run;


  data _null_;

    set stats;

    call symput('avg_cap', avg_capacity);

  run;

  %put Average Capacity in &city: &avg_cap;

%mend;


%region_report(Mumbai);

Log:

Number of Cylinders in Mumbai: 1

Average Capacity in Mumbai: 14.2


%region_report(Surat);

Log:

Number of Cylinders in Surat: 2

Average Capacity in Surat: 50


Step 7: Macro for Observation Count in Any Dataset        

%macro obs_count(dsn);

  %local obs;

  proc sql noprint;

    select count(*) into :obs from &dsn;

  quit;

  %put Number of observations in &dsn: &obs;

%mend;


%obs_count(cylinders);

Log:

Number of observations in cylinders: 15


Step 8: DATA Step Example: Flag Risky Cylinders           

data risky_cylinders;

  set cylinders;

  if Safety_Feature in ("Flame_Retardant","Translucent_Body") then Risky_Flag=1;

  else Risky_Flag=0;

run;


proc print data=risky_cylinders;

  where Risky_Flag=1;

  title 'Cylinders with Elevated Safety Risk';

run;

Output:

Cylinders with Elevated Safety Risk

ObsCylinder_IDTypeMaterialUseLocationSafety_FeatureCapacityRisky_Flag
3C003LPG_CommercialSteelRestaurantChennaiFlame_Retardant191
5C005Mini_LPGCompositeVendorPatnaTranslucent_Body21
14C014WeldedBodySteelManufacturingHyderabadFlame_Retardant651

Step 9: Using Macro Variables Created with CALL SYMPUT    

data _null_;

  set cylinders end=last;

  if last then call symput('last_capacity', Capacity);

run;

%put Last record capacity: &last_capacity;

Log:

Last record capacity: 425


Step 10: Reporting with Custom Macro                      

%macro type_capacity_report(type);

  proc sql noprint;

    select mean(Capacity) into :type_avg

    from cylinders

    where Type="&type";

  quit;

  %put Average Capacity for &type: &type_avg;

%mend;

%type_capacity_report(Fiber_LPG);

Log:

Average Capacity for Fiber_LPG: 7.5


Step 11: PROC FORMAT for Cylinder Type Description        

proc format;

  value $typfmt

    'LPG_Domestic'='Domestic LPG'

    'LPG_Commercial'='Commercial LPG'

    'Mini_LPG'='Mini LPG'

    'Fiber_LPG'='Fiber Composite LPG'

    'Industrial_Type1'='Type 1 Industrial'

    'Industrial_Type2'='Type 2 Industrial'

    'Hydraulic_Single'='Single Acting Hydraulic'

    'Hydraulic_Double'='Double Acting Hydraulic'

    'Telescopic'='Telescopic Hydraulic'

    'TieRod'='Tie Rod Hydraulic'

    'WeldedBody'='Welded Body Hydraulic'

    'LOT_Cylinder'='Liquid Off-Take Cylinder'

  ;

run;


proc print data=cylinders;

  format Type $typfmt.;

  title 'Cylinders with Formatted Type';

run;

Output:

Cylinders with Formatted Type

ObsCylinder_IDTypeMaterialUseLocationSafety_FeatureCapacity
1C001Domestic LPGSteelHomeDelhiPressure_Valve14.2
2C002Domestic LPGSteelHomeMumbaiPressure_Valve14.2
3C003Commercial LPGSteelRestaurantChennaiFlame_Retardant19.0
4C004Commercial LPGSteelHotelKolkataPressure_Valve47.5
5C005Mini LPGCompositeVendorPatnaTranslucent_Body2.0
6C006Fiber Composite LPGCompositeHomeNagpurPressure_Valve5.0
7C007Fiber Composite LPGCompositeHotelKanpurOverpressure_Relief10.0
8C008Type 1 IndustrialSteelFactoryPuneOverpressure_Relief50.0
9C009Type 2 IndustrialAluminumFactorySuratPressure_Valve60.0
10C010Single Acting HydraulicSteelConstructionAhmedabadOverpressure_Relief45.0
11C011Double Acting HydraulicAluminumMachineryBengaluruPressure_Valve60.0
12C012Telescopic HydraulicSteelDumpTruckJaipurOverpressure_Relief80.0
13C013Tie Rod HydraulicSteelManufacturingSuratPressure_Valve40.0
14C014Welded Body HydraulicSteelManufacturingHyderabadFlame_Retardant65.0
15C015Liquid Off-Take CylinderSteelPlantGurgaonHDPE_Tube425.0

Step 12: Subsetting Observations                          

data high_capacity;

  set cylinders;

  if Capacity >= 50;

run;


proc print data=high_capacity;

  title 'High Capacity (>50) Cylinders';

run;

Output:

High Capacity (>50) Cylinders

ObsCylinder_IDTypeMaterialUseLocationSafety_FeatureCapacity
1C008Industrial_Type1SteelFactoryPuneOverpressure_Relief50
2C009Industrial_Type2AluminumFactorySuratPressure_Valve60
3C011Hydraulic_DoubleAluminumMachineryBengaluruPressure_Valve60
4C012TelescopicSteelDumpTruckJaipurOverpressure_Relief80
5C014WeldedBodySteelManufacturingHyderabadFlame_Retardant65
6C015LOT_CylinderSteelPlantGurgaonHDPE_Tube425

Step 13: Merge Example: Adding Inspection Dates           

data inspections;

  input Cylinder_ID $ Last_Inspection :date9.;

  format Last_Inspection date9.;

  datalines;

C001 10JAN2025

C005 05MAR2025

C008 20APR2025

C010 11JUN2025

C012 12AUG2025

C013 07JUL2025

;

run;

proc print;run;

Output:

ObsCylinder_IDLast_Inspection
1C00110JAN2025
2C00505MAR2025
3C00820APR2025
4C01011JUN2025
5C01212AUG2025
6C01307JUL2025


data cylinders_full;

  merge cylinders inspections;

  by Cylinder_ID;

run;


proc print data=cylinders_full;

  title 'Cylinders with Last Inspection Date';

run;

Output:

Cylinders with Last Inspection Date

ObsCylinder_IDTypeMaterialUseLocationSafety_FeatureCapacityLast_Inspection
1C001LPG_DomesticSteelHomeDelhiPressure_Valve14.210JAN2025
2C002LPG_DomesticSteelHomeMumbaiPressure_Valve14.2.
3C003LPG_CommercialSteelRestaurantChennaiFlame_Retardant19.0.
4C004LPG_CommercialSteelHotelKolkataPressure_Valve47.5.
5C005Mini_LPGCompositeVendorPatnaTranslucent_Body2.005MAR2025
6C006Fiber_LPGCompositeHomeNagpurPressure_Valve5.0.
7C007Fiber_LPGCompositeHotelKanpurOverpressure_Relief10.0.
8C008Industrial_Type1SteelFactoryPuneOverpressure_Relief50.020APR2025
9C009Industrial_Type2AluminumFactorySuratPressure_Valve60.0.
10C010Hydraulic_SingleSteelConstructionAhmedabadOverpressure_Relief45.011JUN2025
11C011Hydraulic_DoubleAluminumMachineryBengaluruPressure_Valve60.0.
12C012TelescopicSteelDumpTruckJaipurOverpressure_Relief80.012AUG2025
13C013TieRodSteelManufacturingSuratPressure_Valve40.007JUL2025
14C014WeldedBodySteelManufacturingHyderabadFlame_Retardant65.0.
15C015LOT_CylinderSteelPlantGurgaonHDPE_Tube425.0.

Step 14: SQL Join Example                                 

proc sql;

  create table inspected_composite as

  select a.*, b.Last_Inspection

    from cylinders as a left join inspections as b

      on a.Cylinder_ID = b.Cylinder_ID

    where a.Material = 'Composite';

quit;


proc print data=inspected_composite;

  title 'Composite Cylinders Inspection Details';

run;

Output:

Composite Cylinders Inspection Details

ObsCylinder_IDTypeMaterialUseLocationSafety_FeatureCapacityLast_Inspection
1C005Mini_LPGCompositeVendorPatnaTranslucent_Body205MAR2025
2C006Fiber_LPGCompositeHomeNagpurPressure_Valve5.
3C007Fiber_LPGCompositeHotelKanpurOverpressure_Relief10.

Step 15: Macro Loop and Summary for All Locations         

proc sql noprint;

  select distinct Location into :all_cities separated by ' '

    from cylinders;

quit;


%macro city_loop;

  %local i city;

  %do i=1 %to %sysfunc(countw(&all_cities));

    %let city = %scan(&all_cities, &i);

    %region_report(&city);

  %end;

%mend;

%city_loop;

Log:

Number of Cylinders in Ahmedabad: 1

Average Capacity in Ahmedabad: 45

Number of Cylinders in Bengaluru: 1

Average Capacity in Bengaluru: 60

Number of Cylinders in Chennai: 1

Average Capacity in Chennai: 19

Number of Cylinders in Delhi: 1

Average Capacity in Delhi: 14.2

Number of Cylinders in Gurgaon: 1

Average Capacity in Gurgaon: 425

Number of Cylinders in Hyderabad: 1

Average Capacity in Hyderabad: 65

Number of Cylinders in Jaipur: 1

Average Capacity in Jaipur: 80

Number of Cylinders in Kanpur: 1

Average Capacity in Kanpur: 10

Number of Cylinders in Kolkata: 1

Average Capacity in Kolkata: 47.5

Number of Cylinders in Mumbai: 1

Average Capacity in Mumbai: 14.2

Number of Cylinders in Nagpur: 1

Average Capacity in Nagpur: 5

Number of Cylinders in Patna: 1

Average Capacity in Patna: 2

Number of Cylinders in Pune: 1

Average Capacity in Pune: 50

Number of Cylinders in Surat: 2

Average Capacity in Surat: 50



To Visit My Previous Proc  Means And Nway Option:Click Here
To Visit My Previous Proc Means And CharType Option:Click Here
To Visit My Previous SAS Functions:Click Here
To Visit My Previous Length Statement Using In Many Ways:Click Here



Follow Us On : 


 


--- FOLLOW OUR BLOG FOR MORE INFORMATION.

--->PLEASE DO COMMENTS AND SHARE OUR BLOG.

No comments:

Post a Comment