将excel中的数据读取到SAS中并将字符转换为数字

发布于 2025-01-11 17:59:44 字数 187 浏览 0 评论 0原文

我正在从 SAS 中的 Excel 文件读取数据并将这些值插入到 Oracle 表中。 oracle 表有一个数字列。如果 Excel 文件有数字,则可以正常工作。但如果该列在 Excel 文件中留空,则会被读取为字符值,并且插入到 oracle 会失败。

如果列为空,是否可以将其转换为数字,但如果其有数字,则按原样读取?

谢谢!

I am reading data from an excel file in SAS and inserting the values to an oracle table. The oracle table has a numeric column. If the excel file has numbers, it works fine. But if the column is left blank in the excel file, it is read as a character value and insertion to oracle fails.

Is it possible to convert the column to numeric if its is blank, but read it as is if its has a number?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寂寞清仓 2025-01-18 17:59:44

假设 SAS 将此列作为字符读取,并且您无法直接在文件中对其进行转换。有时会发生这种情况:也许您没有授权执行此操作,或者它只是没有按照您的预期工作。 SAS 可以使用两个函数从字符转换为数字以及从数字转换为字符:input()put()

从字符到数字:input()

input() 用于将字符数据in更改为数字。

这非常适合读取日期、货币、逗号分隔的数字等。如果您需要数字形式的数据,请使用此函数。它的语法是:

num_var = input(char_var, informat.);

在您的情况下,假设我们总是期望数字在这里,即使它丢失了。我们将使用 8. 信息来表示我们感兴趣的变量 my_var

data want;
    set have_excel(rename=(my_var = my_var_char) );
    
    my_var = input(my_var_char, 8.);
    
    drop my_var_char;
run;

请注意,我们需要创建一个新变量。我们将感兴趣的变量重命名为其他名称,然后创建感兴趣的变量的新版本(数字)。在 SAS 中,就像许多其他语言和数据库系统一样,当变量被声明为字符或数字时,它始终是字符或数字。

从数字到字符:put()

put() 用于将数字放入字符或一个字符到另一个字符。

这对于将 SAS 日期转换为字符、添加自定义格式、将一个字符转换为另一个字符等非常有用。语法为:

char_var = put(num_var, format.);

或:

char_var = put(char_var, format.);

请注意前面的用例:使用 put(),您可以将字符转换为其他字符。这对于标准化值甚至使用格式合并数据非常方便。

例如:让我们将数字转换为逗号分隔的字符数字。

data want;
    char_number = put(1234, comma.);
run;

输出:

char_number
1,234

Let's assume that SAS is reading this column as a character and you cannot convert it directly within the file. This happens sometimes: maybe you don't have authorization to do it, or maybe it's just not working like you're expecting. SAS can go from character to numeric and numeric to character with two functions: input() and put().

Going from Character to Numeric: input()

input() is for changing character data into numbers.

This is great for reading in dates, currency, comma-separated numbers, etc. If you need your data as a number, use this function. Its syntax is:

num_var = input(char_var, informat.);

In your case, let's say we always expect numbers to be here even if it's missing. We'll use the 8. informat for our variable of interest, my_var.

data want;
    set have_excel(rename=(my_var = my_var_char) );
    
    my_var = input(my_var_char, 8.);
    
    drop my_var_char;
run;

Note that we need to create a new variable. We rename the variable of interest to something else, then create a new version of the variable of interest that is a number. In SAS, just like many other languages and database systems, when a variable is declared as a character or number, it is always a character or a number.

Going from Numeric to Character: put()

put() is for putting a number to a character or a character to another character.

This is great for converting SAS dates to characters, adding custom formats, converting a character to another character, etc. The syntax is:

char_var = put(num_var, format.);

OR:

char_var = put(char_var, format.);

Note the previous use case: with put(), you can convert characters to other characters. This is very handy for standardizing values or even merging data using a format.

For example: let's convert a number to a comma-separated character number.

data want;
    char_number = put(1234, comma.);
run;

Output:

char_number
1,234
不奢求什么 2025-01-18 17:59:44

下面的案例陈述对我有用。

case 
when missing(input(cats(COLUMN_VALUE), best8.)) THEN input(cats(COLUMN_VALUE), best8.) 
when not missing(input(cats(COLUMN_VALUE), best8.)) THEN input(cats(COLUMN_VALUE), best8.) 
end as COLUMN_VALUE

Below case statement worked for me.

case 
when missing(input(cats(COLUMN_VALUE), best8.)) THEN input(cats(COLUMN_VALUE), best8.) 
when not missing(input(cats(COLUMN_VALUE), best8.)) THEN input(cats(COLUMN_VALUE), best8.) 
end as COLUMN_VALUE
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文