BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Paige1
Fluorite | Level 6

I create a format using proc format cntlin:

 

 

data Fmt(keep=FMTNAME TYPE START LABEL HLO);
	retain FMTNAME 'Fmt' type 'C';
	set dataset END=EOF;
	where CitShare ne .;
	start=market;
	label=CitShare;
	output;
	if EOF;
	HLO='O';
	LABEL='.';
	output;
run;

proc format cntlin=Fmt library=work;
run;

Everything looks good. The label variable in the Fmt dataset is numeric.
But later when I use the format, it converts to numeric. 

data cit_join;
	set cit_markets;
	CitShare=put(market,$Fmt.);
run;

cit_join has CitShare as a character variable. 

What am I doing wrong? How can I make it stay numeric?

I even try to then convert it to numeric and it doesn't work:

data join;
set join;
	CitShare = input(CitShare, best32.);
run;

log reads: 
NOTE: Numeric values have been converted to character values at the places given by: (Line):(Column).
31:13

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Quentin
Super User

The PUT function ALWAYS returns a character value.  That's it's job.

 

If you want to convert a character value to a numeric value, you need to use the INPUT function with a numeric informat.

 

You could use PROC format to create a numeric informat, or you could use your current character format like (untested):

data cit_join;
	set cit_markets;
	CitShare=input(put(market,$Fmt.),best32.);
run;

Your second attempt at this:

data join;
set join;
	CitShare = input(CitShare, best32.);
run;

likely didn't work because CitShare already existed in the dataset join as a character variable.  So the input function returned a numeric value, but then SAS had to force that value into a character variable so it converted the numeric value to character, and threw the NOTE you see in the log.  You can't actually *change* the type of a variable in SAS.  Your second approach would have worked if you renamed the character variable when you read it in, something like:

 

data myjoin; *bad idea to read and write the same dataset, so I changed this name;
set join (rename=(CitShare=_CitShare));
	CitShare = input(_CitShare, best32.);
run;

 

 

BASUG is hosting free webinars Next up: Mark Keintz presenting History Carried Forward, Future Carried Back: Mixing Time Series of Differing Frequencies on May 8. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.

View solution in original post

2 REPLIES 2
Quentin
Super User

The PUT function ALWAYS returns a character value.  That's it's job.

 

If you want to convert a character value to a numeric value, you need to use the INPUT function with a numeric informat.

 

You could use PROC format to create a numeric informat, or you could use your current character format like (untested):

data cit_join;
	set cit_markets;
	CitShare=input(put(market,$Fmt.),best32.);
run;

Your second attempt at this:

data join;
set join;
	CitShare = input(CitShare, best32.);
run;

likely didn't work because CitShare already existed in the dataset join as a character variable.  So the input function returned a numeric value, but then SAS had to force that value into a character variable so it converted the numeric value to character, and threw the NOTE you see in the log.  You can't actually *change* the type of a variable in SAS.  Your second approach would have worked if you renamed the character variable when you read it in, something like:

 

data myjoin; *bad idea to read and write the same dataset, so I changed this name;
set join (rename=(CitShare=_CitShare));
	CitShare = input(_CitShare, best32.);
run;

 

 

BASUG is hosting free webinars Next up: Mark Keintz presenting History Carried Forward, Future Carried Back: Mixing Time Series of Differing Frequencies on May 8. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.
Tom
Super User Tom
Super User

You want an INFORMAT not a FORMAT.

...
retain FMTNAME 'Fmt' type 'I';
...
CitShare=INPUT(market,Fmt.);
...

FORMATs convert values to text.  INFORMATs convert text to values.

Numeric formats convert numeric values to text.

Character formats convert character values to text.

Numeric informats convert text to numeric values .

Character informats convert text to character values .

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!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 2 replies
  • 1349 views
  • 3 likes
  • 3 in conversation