Multiplying by 1 does not change a variable type.
You can use arrays to make a change across multiple variables but you need to list the variables somehow.
data convert;
set have;
array new(*) new1-new20;
array old(*) $ old1-old20;
do i=1 to dim(old);
new(i) = input(old(i), 8.);
end;
run;
If you have a few in there that are of mixed types then that gets to be a pain the a** because arrays can only be one type.
You can use the methods here to list the variables easier:
Here is a reference that illustrates how to refer to variables and datasets in a short cut list:
https://blogs.sas.com/content/iml/2018/05/29/6-easy-ways-to-specify-a-list-of-variables-in-sas.html
Here's a tutorial on using Arrays in SAS
https://stats.idre.ucla.edu/sas/seminars/sas-arrays/
@xyxu wrote:
I need to ensure all variables with the same prefix are numeric, so want to multiply their values by 1. Is there a good way to do this?
This makes sense. The only issue with using the data step to import csv format is that the file has hundreds of columns with the same prefix, each followed by a different date. I am still trying to find a way to avoid typing all these columns in the informat command.
This is working now. The data step has 2000 lines though. The only issue I am having is that my numeric values have a comma as the thousand separator, and SAS gets confused.
I am using
data want;
infile "&in_path\data.csv" delimiter = ',' dsd missover lrecl = 32767 firstobs = 2;
informat variables 15.2;
format variables;
input variables;
run;
All other variables are read correctly, except for ones that have thousand separators.
@xyxu wrote:
I opened it in Atom. The values look like the following: "1,972,909.44","1,771,118.01","1,710,921.05","1,604,967.14","1,655,117.54","1,638,147.95"
So variables that have commas in numeric values are read as COMMA16. (or possibly larger width)
Example:
data example; infile datalines dlm=',' dsd; informat var1-var6 comma16.; input var1-var6; datalines; "1,972,909.44","1,771,118.01","1,710,921.05","1,604,967.14","1,655,117.54","1,638,147.95" ;
You can look at the variables created. The difference is your code would point to the external file on the INFILE statement instead of the inline data.
If you later want the variables to display with commas then assign a format like Comma18.2;
I typically add a couple of additional characters for the informat width in case your example didn't capture the largest values. Do NOT specify the decimal portion on the Informat you can get very interesting (and usually wrong results) unless you really understand what implied decimal means when the value to be read is "123" in the text file.
@xyxu wrote:
This makes sense. The only issue with using the data step to import csv format is that the file has hundreds of columns with the same prefix, each followed by a different date. I am still trying to find a way to avoid typing all these columns in the informat command.
Show some actual names needed. Or the header of the csv with the column headers. If you go this way copy the text from the CSV file with a text editor NOT any spread sheet software as that often interprets stuff, then paste the resulting text into a text box opened on the forum with the </> icon above message window.
If there is an obvious pattern, that can likely be coded to write the names needed.
It is also not terribly difficult to read one line of text from file, such as the header row of a csv file and then fold, spindle and mutilate to get the text you need and use it to write the Informat information needed which could be %included as needed.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.