BookmarkSubscribeRSS Feed
sayanapex06
Obsidian | Level 7

I have the following code :

 

DATA XXX ;
LENGTH CC $2. FLG $1. ;
INFILE DATALINES DLM = '~' ;
INPUT @1 CC FLG ;
DATALINES ;
AB~Y
BC~N
CD~Y
DF~N
TY~N
;

DATA CREATE_FORMAT ;
RETAIN FMTNAME 'TESTFR' TYPE 'C' ;
SET XXX END=EOF ;
START = CC ;
LABEL = FLG ;
OUTPUT ;
IF EOF ; /* ON LAST RECORD */
HLO = 'O' ;
LABEL = 'X' ;
OUTPUT ;
RUN ;

LIBNAME LIBRARY '/sas/data/DEV_TEST/SAYAN_TEST/' ;

PROC FORMAT LIBRARY = LIBRARY CNTLIN = CREATE_FORMAT ;



RUN ;

 

I am not getting the desired output which should look like :

 

PROC FORMAT ;

VALUE $TESTFR 

 'AB' = 'Y' 

...........(etc)

 

I am getting it in binary form.

 

Pleas help me with getting the desired output.

 

Thanks,

Sayan

10 REPLIES 10
Reeza
Super User

I think you're possibly misunderstanding the use of CNTLIN...it doesn't create a text file, it creates the format that can be used, so what would happen after you ran the PROC FORMAT. Is there any specific reason you need the PROC FORMAT code as shown? You can use CNTLOUT to generate the data set back out if needed....but it still wouldn't be in that style.

 


@sayanapex06 wrote:

I have the following code :

 

DATA XXX ;
LENGTH CC $2. FLG $1. ;
INFILE DATALINES DLM = '~' ;
@input @1 CC FLG ;
DATALINES ;
AB~Y
BC~N
CD~Y
DF~N
TY~N
;

DATA CREATE_FORMAT ;
RETAIN FMTNAME 'TESTFR' TYPE 'C' ;
SET XXX END=EOF ;
START = CC ;
LABEL = FLG ;
OUTPUT ;
IF EOF ; /* ON LAST RECORD */
HLO = 'O' ;
LABEL = 'X' ;
OUTPUT ;
RUN ;

LIBNAME LIBRARY '/sas/data/DEV_TEST/SAYAN_TEST/' ;

PROC FORMAT LIBRARY = LIBRARY CNTLIN = CREATE_FORMAT ;



RUN ;

 

I am not getting the desired output which should look like :

 

PROC FORMAT ;

VALUE $TESTFR 

 'AB' = 'Y' 

...........(etc)

 

I am getting it in binary form.

 

Pleas help me with getting the desired output.

 

Thanks,

Sayan


 

sayanapex06
Obsidian | Level 7

I need it to generalize and shorten a huge process.

 

You are correct even with CNTLOUT, I didnt get the desired format.

 

So could you suggest, how I can try build a format from a SAS dataset in the desired proc format.

 

Thanks,

Sayan

Reeza
Super User

You'd have to do it manually, which is why I don't understand why this would be needed or how it would help.

It's also difficult because you need to account for the possibility of a multilabel format or the other and/or missing values. There's also intervals that need to be accounted for...so it's definitely possible but a set of tedious programming using PUT statements. If you know it's a one to one coding that's a few put statements but you still need to account for the different format types and such. 

 

You can try and search on Lexjansen to see if someone created a macro or code before PROC FORMAT had CNTLIN but you're looking for really old papers at that point. 

 

https://lexjansen.com/search/searchresults.php?q=create%20proc%20format%20code%20from%20data%20set

 

Is there a specific reason, PROC FORMAT with CNTLIN doesn't do exactly what you need? It's more concise and does the exact same thing. I genuinely cannot see the use case (besides an academic) for this exercise. 

 

 

Reeza
Super User

Started in 1997 - more than 2 decades ago! 😄

Tom
Super User Tom
Super User

Not hard the do.

filename code temp;
data _null_;
  file code ;
  set xxx end=eof ;
  if _n_=1 then put 
 'value $testfr'
  ;
  put @3 cc :$quote. '=' flg :$quote. ;
  if eof then put
   @3 'other="X"'
/ ';'
  ;
run;

proc format ;
  %include code / source2;
run;
sayanapex06
Obsidian | Level 7
Yes this is one of the ways we can do.. But I wanna do it with either
CNTLIN /CNTLOUT
Reeza
Super User

Did you see @FreelanceReinh post? That seems to do exactly what you want, if it doesn't please explain how. 

ballardw
Super User

@sayanapex06 wrote:
Yes this is one of the ways we can do.. But I wanna do it with either
CNTLIN /CNTLOUT

Like this? I add a record to your starting data set with a missing value for CC to demonstrate that it gets formatted in output as desired.

 

DATA work.XXX ;
LENGTH CC $2. FLG $1. ;
INFILE DATALINES DLM = '~' ;
INPUT @1 CC FLG ;
DATALINES ;
AB~Y
BC~N
CD~Y
DF~N
TY~N
;

DATA work.CREATE_FORMAT ;
RETAIN FMTNAME '$TESTFR' TYPE 'C' ;
SET work.XXX END=EOF ;
START = CC ;
LABEL = FLG ;
OUTPUT ;
IF EOF ; /* ON LAST RECORD */
HLO = 'O' ;
LABEL = 'X' ;
OUTPUT ;
RUN ;

Proc format library=work cntlin=work.create_format;
run;
data work.test;
   set work.xxx end=eof;
   output;
   if eof then do;
      call missing(cc,flg);
      output;
   end;
run;
    
proc print data=work.test;
  var cc;
  format cc $testfr.;
run;
Astounding
PROC Star

Actually your code looks pretty close.  When creating a character format, the name of the format must begin with a dollar sign.  I'm not sure that setting the format type to "C" tells SAS to do that automatically.  Try:

 

RETAIN FMTNAME '$TESTFR' TYPE 'C' ;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 10 replies
  • 1144 views
  • 2 likes
  • 6 in conversation