DATA Step, Macro, Functions and more

can a character variable be set as format by relating numeric value

Reply
Occasional Contributor mj5
Occasional Contributor
Posts: 18

can a character variable be set as format by relating numeric value

can i make format of character value which has relation to numeric value.

 

DATA temp1;
     input subj 1-4 num_val 6 result $8-20 ;
     DATALINES;
1024 1 manual 
1167 1 manual 
1168 2 testing 
1201 2 testing 
1302 3 plain
1302 4 natural
  ;
RUN;

in the above program i want to use num_val  to get right order and at last want to call result to get the char value.

Can i create a format or one to one relation in the code and call it when needed?

PROC Star
Posts: 8,145

Re: can a character variable be set as format by relating numeric value

Not exactly sure what you're asking but, if it's just how to create a format from that dataset:

ATA temp1;
  input subj 1-4 num_val 6 result $8-20 ;
  DATALINES;
1024 1 manual 
1167 1 manual 
1168 2 testing 
1201 2 testing 
1302 3 plain
1302 4 natural
;

data fmtdata;
  set temp1 (rename=(num_val=start result=label));
  by start;
  retain fmtname 'ncodes' type 'N';
  if first.start;
run;

proc format cntlin = fmtdata;
run;

Art, CEO, AnalystFinder.com

 

Occasional Contributor mj5
Occasional Contributor
Posts: 18

Re: can a character variable be set as format by relating numeric value

How to call this format in data step. I tried calling by format 

format num_val $ncodes.; 

but it doesnt work.

Any clue? 

Respected Advisor
Posts: 4,668

Re: can a character variable be set as format by relating numeric value

@mj5

If you're using @art297's code then the format is numeric with name ncodes - no $ required.

format num_val ncodes.; 

 

Respected Advisor
Posts: 4,668

Re: can a character variable be set as format by relating numeric value

@mj5

Yes, that's one of the main purposes of formats. You apply them the values so that they print/display formatted.

Just define the format like:

proc format;
  value myformat
    1='manual'
    2='testing'
    ....
    ;
run;

You then can apply the format like:

DATA temp1;
     input subj 1-4 num_val 6 result $8-20 ;
     format num_val myformat.;
     DATALINES;
1024 1 manual 
....
;
run;

In doing so you will see the formatted values when "looking at them" but the internal values are still 1, 2, 3... and that's what you have to use in your SAS code for selections and the like.

Ask a Question
Discussion stats
  • 4 replies
  • 129 views
  • 2 likes
  • 3 in conversation