Hi All,
I have the following spreadsheet I want to import. During the import stage I also want to Rename, Retain and Label the column. Is it possible to implement all three functions during the import stage? Thanks
ID Name1 Name2 Title1 Title2
1234 Alex Michael 23 New York St. (123) 563-2541
5678 John Jacob 202-74th St. 123-585-4785
3456 Heather-Bin Tornoway 220- 35th Ave. (123)4587458
2345 Megan-Fitzam Hansen 9 Huron st. (123)4587458
;
run;
I came up with the following code:
proc import datafile="C:\Have.xlsx"
dbms=xlsx replace
out=Want (RENAME=(ID=St_ID Name1=First_name Name2=Last_name Title1=Address) keep = ID Name1 Name2 Title1);
sheet='Sheet1';
run;
Expected Output:
St_ID First_name Last_name Address
1234 Alex Michael 23 New York St.
5678 John Jacob 202-74th St.
3456 Heather-Bin Tornoway 220- 35th Ave.
2345 Megan-Fitzam Hansen 9 Huron st.
Use the following statement (it works even in my PROC IMPORT step):
attrib _all_ label=' ';
Save the spreadsheet as a CSV file. Use Proc import to generate a basic datastep to read the data, copy the code from the log and add any changes desired. For instance to a search and replace on the default variable names to the name you want. Add label code.
I'm not quite sure what you want to "retain" but since this is a data step you can any datastep code you desire.
If you are happy with the contents of your data and just need to change names and add labels then Proc Datasets is the tool designed to do both to a set in place without re-reading any data or creating an additional dataset.
@mlogan wrote:
After renaming the dataset during import stage, it still keep the LABEL as it was in the excel spreadsheet. I wanted to keep label same as my new variable name (after renaming).
Are you saying that the LABEL statement in PROC IMPORT is not effective? (I cannot test it with dbms=xlsx, unfortunately, because I don't have Excel installed, but it works with dbms=dlm.)
Also, wouldn't labels equal to the variable names add only little value since in most cases variable names would be used anyway by default for display if no labels were present (so you could simply remove all labels)?
As to the order of variables in the dataset (which is rarely relevant), I would be surprised if this could be arranged with PROC IMPORT alone. No problem, of course, with a data step reading the raw data.
Use the following statement (it works even in my PROC IMPORT step):
attrib _all_ label=' ';
I'd put it just before the RUN statement.
Glad to hear that it worked. I was also surprised by the incorrect syntax highlighting in this case.
I have it right before RUN. Do you receive error messages or in which way does your step fail?
Hi @mlogan,
You can also insert a LABEL statement into the PROC IMPORT step:
label St_ID = 'Student ID'
First_name = 'First name'
/* ... */
;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.