Help using Base SAS procedures

Format

Reply
Regular Contributor
Posts: 190

Format

data details;                                                                                                                             

   input start $ 1-2 label $50. ;                                                                                         

   datalines;   

1 AFGAN

2 MULTIPLE

3 OTHER

;

RUN;

am new to formats how can i create a macro with input dataset as parameter & create format from the dataset details!  help ?

Super User
Posts: 6,928

Re: Format

You just need two additional columns in your dataset:

data details;
input start $ 1-2 label $50.;
fmtname = 'xnational';
type = 'C';
datalines;
1 AFGAN
2 MULTIPLE
3 OTHER
;
run;

proc format library=work cntlin=details;
run;

data want;
nationality = put("1",$xnational.);
run;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Regular Contributor
Posts: 190

Re: Format

can i code this in a macro & how can i test it for all values in the details dataset?
Respected Advisor
Posts: 3,777

Re: Format

Are you asking if you can use the FORMAT as a source for valid values for data cleaning?

Super User
Posts: 9,662

Re: Format

No need for Macro.

 

data details;                                                                                                                             
   input start $ 1-2 label $50. ;
   fmtname='fmt'; 
   type='n';                                                                                        
   datalines;   
1 AFGAN
2 MULTIPLE
3 OTHER
;
RUN;
proc format cntlin=details;run;

data _NULL_;
 test=1; fmt=put(test,fmt.); putlog test= fmt=;
 test=2; fmt=put(test,fmt.); putlog test= fmt=;
 test=4; fmt=put(test,fmt.); putlog test= fmt=;
run;
Regular Contributor
Posts: 190

Re: Format

@ xiabut the data is huge so need a macro %macro fmt(inds=);
Super User
Posts: 6,928

Re: Format

No matter how big a dataset is, the code stays the same. NO need for a macro.

Unless you have other requirements that you did not mention.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Regular Contributor
Posts: 190

Re: Format

ok let me rephrase %macro fmt(inds=); is my requirement to do the code.
Super User
Posts: 6,928

Re: Format

What is the inds parameter in the macro? The dataset that contains the layout of the format, or the dataset that the format should be applied on?

 

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Regular Contributor
Posts: 190

Re: Format

DS that the format should be applied on?
Super User
Posts: 6,928

Re: Format

Then you need more parameters. Which column(s) should which format be applied to?

In the end you may find that writing a macro that solves all cases will be more effort than simply writing the format assignments by hand.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Posts: 5,072

Re: Format

Unfortunately, you have a difficult task.  You will need to go back to the person who gave you the requirements and tell them that they have no idea what they are talking about.

 

If you look at output data sets based on a format, there are roughly 20 variables.  That means you will need roughly 20 parameters on such a macro, depending on which features of a format you would like to utilize.  There is no way that a single parameter can do that.  If you can narrow down the features that are needed, you can reduce the number of parameters.  But you have to start with a list of features.

Ask a Question
Discussion stats
  • 11 replies
  • 553 views
  • 1 like
  • 5 in conversation