Creating Groups Using PROC FORMAT | Real Time Problems

Problem/Requirement -

Lets Suppose, We have Data in Numbers which could be range from 100 to 10000
now we want to create Groups of Each 100 Values, like below - 

Groups Example in SAS



Lets See with the Example | The Power of the FORMAT Procedure - 


/*This code will Generate some Numbers between 100 and 10000 to Get the Practice Dataset */

Data A;
Num=100;
Do while(Num<10000);
output;
Num=Num+54;
end;
Run;
Proc Print Data=A;
Run;

Creating Groups in SAS


Now we want to Group it in as per given in Requirement 

/* This Code will Genrate FMTName (Format Name), Start(Starting Number of Group), End(Ending Number of Group), Label(Group name for Numbers between Start and End Values)
*/
Data B;
Retain FMTName "Group";
Start=100;
i=1;
Do until(End>=10000);
End=Start+99;
Label=compbl("Group"||i);
Output;
Start=End+1;
i=i+1;
end;
drop i;
run;

Proc Print Data=B;
Run;

Grouping Data in SAS

Now if we need to create a User Defined Format -

/* This wil create a format for Above created B Dataset with Format Name "Group", PROC FORMAT with the CNTLIN= option is to build the formats from the imported control data set */

Proc Format CNTLIN=B;
Run; 

/*Lets see how it will print A Dataset if we apply Newly create Group format to Variable Num */

Proc Print Data=A;
Format Num Group.;
run; 

USE of Proc Format in SAS
/* If we want Actual Number and the Group name both the things then we can do it like this */

Data C;
Set A;
GroupName=Put(Num,Group.);
run;
/*Here we created a new Variable Group name and Copy value of Num with Group format, For this we used PUT Function */
Proc Print Data=C;
Run;
USE of Proc Format in SAS


I Hope you like this Article, please comment if you have any doubt or question.
Thank you

Post a Comment

0 Comments