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

Hello Experts,

 

Please what is the best way to insert a column name row into a SAS dataset. It is a part of a  larger file that was split into smaller parts for ease of transfer (I don't want to merge the files). See sample below i.e name the columns in file2

HAVE     WANT     
FILE1     FILE1    
IDdate Brand CodeTotal transactionValue($) IDdate Brand CodeTotal transactionValue($)
003031/1/20201156612345000 003031/1/20201156612345000
052211/1/2020382207702000 052211/1/2020382207702000
449901/1/20207648935677300 449901/1/20207648935677300
           
FILE2     FILE2    
077731/11/20203513923453350 IDdate Brand CodeTotal transactionValue($)
222221/11/202060275167470 077731/11/20203513923453350
100501/11/202024677200500 222221/11/202060275167470
      100501/11/202024677200500

 

Thank you,

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
Then you need to fix your data import step instead, not after the fact. I'm assuming you specified firstobs=1 and used a data step. As I'm sure you're aware, PROC IMPORT will not work for files of this structure. Or go back and fix how you split the file so it writes headers to each file as well - this is the optimal solution to avoid issues but you'll still want a data step. Otherwise when it comes time to combine these datasets you'll have mismatch of types and that will cause other issues.

View solution in original post

8 REPLIES 8
PaigeMiller
Diamond | Level 26

I'm afraid your data doesn't make sense to me.

 

In FILE2, the HAVE data set has no variable names, which is impossible, all SAS data sets have variable names. Please explain.

--
Paige Miller
michokwu
Quartz | Level 8

The file was split into smaller csv files. When imported into SAS, the first observation is interpreted as variable names.

Reeza
Super User
Then you need to fix your data import step instead, not after the fact. I'm assuming you specified firstobs=1 and used a data step. As I'm sure you're aware, PROC IMPORT will not work for files of this structure. Or go back and fix how you split the file so it writes headers to each file as well - this is the optimal solution to avoid issues but you'll still want a data step. Otherwise when it comes time to combine these datasets you'll have mismatch of types and that will cause other issues.
michokwu
Quartz | Level 8

The files were sent by someone else. I've fixed it. I unchecked the box 'first row of range contains field names' 

Reeza
Super User
If you don't use a data step you''ll like end up with the type inconsistency issue. You really need to fix it.
michokwu
Quartz | Level 8

@PaigeMiller You are right, if the 'first row of range contains field names' box is unchecked, the variables are automatically named F1,F2..........

Reeza
Super User
So you have at least one dataset with the correct names? Are the positions the same between all versions of the data set?

Are you 100% sure you had to split your data set and/or how did you do that? Ideally you'll go back and make sure it's happening correctly at that stage but renaming is relatively straightforward once you clarify the rules. If you're certain all the file structures are exactly the same you can use PROC DATASETS to easily update all your datasets. But do you want variable names or labels is something else you should consider. Do you want to have 'Brand Code'n as your variable name or BrandCode and a label of "Brand Code"?

proc datasets lib=work nodetails nolist;
modify want;
rename var1=ID var2 = Date var3 = 'Brand Code'n var4 = 'Total Transaction'n var4 = 'Value($)'n;
run;quit;
ballardw
Super User

I am very confused about splitting a file to "transfer" it but not wanting a single file. If the sole purpose of the two files is to append them back together then read them correctly to begin with. You can read multiple files with a single data step. Sort of an example:

filename toread ("c:\path\file1.csv" "c:\path\file2.csv" );
data want;
   infile toread dlm=',' dsd firstobs=2;
   informat id $6. date mmddyy10. brand $6. code $5.
           total  value best12.;
   format date mmddyy10.;
   informat id  date  brand  code
           total  value ;
run;

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 8 replies
  • 6676 views
  • 4 likes
  • 4 in conversation