BookmarkSubscribeRSS Feed
Mayt
Quartz | Level 8

Hi everyone

Do you know how to set column type as int in sas studio? right now it is double.

My code :data gems;

data gems;
input Name $ Carats comma3. Color $char10.;
informat Name $char10.;
format Carats comma3.;
datalines;
emerald 15green
aquamarine 20 blue
;
proc print data=gems; run;
proc contents data=gems; run;

 

PROC SQL;
CREATE TABLE cas_sme.gems AS SELECT * FROM 
work.gems;
QUIT;
PROC CASUTIL;
SAVE CASDATA = "gems" INCASLIB = "cas_sme" OUTCASLIB = "cas_sme" 
CASOUT = "gems.sashdat" REPLACE;
QUIT;

Result : 

Mayt_0-1685632883883.png

Mayt_3-1685633082220.png

Moreover, I have tried Int function but still column type is double.

data r ;
 
     set work.gems ;
 
     y = round(Carats,1);
 
     z1 = Int(Carats);
 
     z2 = Intz(Carats);
run;
PROC SQL;
CREATE TABLE cas_sme.gems AS SELECT * FROM 
work.r;
QUIT;
PROC CASUTIL;
SAVE CASDATA = "gems" INCASLIB = "cas_sme" OUTCASLIB = "cas_sme" 
CASOUT = "gems.sashdat" REPLACE;
QUIT;
 
Result:
Screenshot 2023-06-01 224640.pngScreenshot 2023-06-01 224709.png

 

10 REPLIES 10
mkeintz
PROC Star

But under PROC DS2, CAS data tables can store INT64 and INT32.  See Data Types for CAS in the SAS DS2 Language Reference.

 

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
ballardw
Super User

You need to test that your data step reads correctly before providing it to us.

 

If you use an informat like 3.1 on an input statement it will force a decimal. Do you intend to read that 15 for emerald as 1.5?

Then:

data gems;
   input Name $10. Carats :3.1 Color $char10.;
   informat Name $char10.;
   format Carats comma3.1;
datalines;
emerald    15 green
aquamarine 20 blue
;

Your code read the carats for emerald as part of the Color value. When using list input with a format like comma3.1 or 3.1 SAS starts reading immediately after the end of the previous value so the spaces were read as the carats.

 

And why is there any concern about "integer" value when you are apparently forcing at least one decimal to be part of the value????

Why the Comma3.1 ? Forcing one decimal means that 2 of the 3 character positions possible are the decimal and whatever digit follows. Do you have data with a currency symbol followed by decimal? Any value that actually has comma in it would be a minimum of 5 characters.

Mayt
Quartz | Level 8

My bad.. I have corrected my code as the updated code on the top, but the carats column is still double.

ballardw
Super User

@Mayt wrote:

My bad.. I have corrected my code as the updated code on the top, but the carats column is still double.


And it will continue to be so because there is no such thing as an "integer" variable type.

 

 

You data step code now throws an invalid data error:

data gems;
input Name $ Carats comma3. Color $char10.;
informat Name $char10.;
format Carats comma3.;
datalines;
emerald 15green
aquamarine 20 blue
;

With no space between the 15 and "green" you have told SAS to read 3 characters and g of green is the third. Which is invalid for the comma3 (and pretty much any SAS supplied numeric informat) And note the value of Color for the first observation. List input expects a space between values. Datalines are a bit pickier in some respects than an Infile statement for reading data and may need to modify informats.

47   data gems;
48   input Name $ Carats comma3. Color $char10.;
49   informat Name $char10.;
50   format Carats comma3.;
51   datalines;

NOTE: Invalid data for Carats in line 52 9-11.
RULE:      ----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+--
52         emerald 15green
Name=emerald Carats=. Color=reen _ERROR_=1 _N_=1

Mayt
Quartz | Level 8

Mayt_0-1685634963451.png

Screenshot 2023-06-01 225955.png

when I added space between 15 and green, there is a space before green in color column. I'm quite confused too why no space in this case work for me.

Tom
Super User Tom
Super User

You have mixed FORMATTED and LIST MODE input style in the same INPUT statement.

 

When you use LIST MODE then SAS reads the next "field" from the line, where field boundaries are defined by the combination of DSD, DLM= and DLMSTR= settings.

 

When you use FORMATTED mode the it just reads exactly the number of bytes you told it to read.

 

You have also used the special $CHAR informat instead of the normal character informat $.  So the leading spaces are preserved.  Another side effect of using $CHAR instead of $ is that you cannot designate a missing character value by just using a period.  When a single period is read by $ the result is a blank value.  But when read by $CHAR the period becomes part of the actual value of the variable.

 

If for some reason you wanted to include an informat specification in the INPUT statement (perhaps you have DATE values to read?) then prefix the informat with the colon modifier to tell INPUT to use LIST MODE.

Tom
Super User Tom
Super User

From the documentation apparently NOT.

 

SAS only has two data types.  Fixed length strings and floating point numbers.  CAS has added a variable length character variable type.   It does not appear to have added any additional numeric data types.

 

Note your example data step cannot work.    NAME is not defined long enough to hold that last value. You are inserting a leading space into one of the values of COLOR.

 

To insure that the variables are defined as you want them define them with a LENGTH statement before using them in statements like INPUT or FORMAT or INFORMAT.  None of your three variables needs special instructions for reading or writing.  SAS already knows how to read and display character strings and numbers.  There is no need to use COMMA format or informat to display 3 digit numbers.  The commas will start to appear for values larger than 999.

data gems;
  length Name $10 Carats 8 Color $10 ;
  input Name Carats Color;
datalines;
emerald 15 green
aquamarine 20 blue
;

 

Mayt
Quartz | Level 8

Mayt_0-1685637079507.png

So sas allows int type only for rulesFiredForRecordCount column in decision test result?

Tom
Super User Tom
Super User

I don't know what CAS allows, just what the documentation page linked above says.

 

Normally when you transfer a SAS dataset to a foreign database SAS will assume any variable that has the normal numeric format permanently attach that specifies zero decimal places is an integer type (or DECIMAL type if the foreign database has such a type).  For systems with DECIMAL type they can even use that when the attached format has decimal places.  

 

So try attaching the normal numeric format 3. instead of the comma3. format your code used to your numeric variable and see what CAS does with it.  You can also use F3. format specification since F is an alias for the normal numeric format.

 

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 10 replies
  • 2412 views
  • 0 likes
  • 5 in conversation