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

Hello Friends,

I have the attached data set with variables name with special character  in csv file such as Q19.1, Q20.1, Q22.1, Q22.2, Q22.3 etc. How can I import these variables as SAS Standard column/variables name? Either in Proc import or in Data step.

Thank you in advance for your help. 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Since it is a text file just use your own names when reading the data.

They don't have to be anything like the column headers.

data want;
  infile "Sample_data.csv" dsd truncover firstobs=2;
  input id $ var1-var11;
run;

You can also make sure to set VALIDVARNAME=V7 and then let PROC IMPORT try to convert the headers into valid names.  But then you lose control over not only how the variables are named but also how they are defined.

 

View solution in original post

2 REPLIES 2
ballardw
Super User

@Akter wrote:

Hello Friends,

I have the attached data set with variables name with special character  in csv file such as Q19.1, Q20.1, Q22.1, Q22.2, Q22.3 etc. How can I import these variables as SAS Standard column/variables name? Either in Proc import or in Data step.

Thank you in advance for your help. 

 


Please see:

data example;
  infile datalines dlm=',';
  input ID :$8. Q19_1 Q20_1 Q22 Q22_1 Q22_2 Q22_3 Q22_4 Q22_5 Q22_6 Q22_7 Q22_8;
datalines;
07-003,3,1,7,0,0,0,0,0,0,1,0
05-028,4,1,8,0,0,0,0,0,0,1,1
06-033,4,1,7,0,0,0,0,0,0,1,0
09-006,3,1,8,0,0,0,0,0,0,0,1
06-017,5,1,6,0,0,0,0,0,1,0,0
06-009,4,1,1,1,0,0,0,0,0,0,1
04-041,6,1,5,0,1,0,0,0,1,1,1
03-028,4,1,8,1,0,0,0,0,0,0,1
06-032,2,1,1,1,0,0,0,0,0,0,0
02-012,5,1,7,0,0,0,0,0,0,1,0
05-014,2,1,5,1,1,0,0,0,1,0,0
07-005,2,1,7,0,0,0,0,0,0,1,0
09-050,2,1,8,0,0,0,0,0,0,0,1
09-011,5,1,4,1,1,0,1,0,0,1,1
;

So to read you data the INFILE statement would have the path and file name in quotes:

Infile "c:\folder\subfolder\sampledata.csv" dlm=',';

and you would not include the DATALINES portion.

Tom
Super User Tom
Super User

Since it is a text file just use your own names when reading the data.

They don't have to be anything like the column headers.

data want;
  infile "Sample_data.csv" dsd truncover firstobs=2;
  input id $ var1-var11;
run;

You can also make sure to set VALIDVARNAME=V7 and then let PROC IMPORT try to convert the headers into valid names.  But then you lose control over not only how the variables are named but also how they are defined.

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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
  • 2 replies
  • 631 views
  • 2 likes
  • 3 in conversation