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

Hi,

I am trying to import a csv file in SAS using data step, but I want SAS read the variables as I order them

e.g.

Have:

Var3Var2Var1
Cat44Grade 1
Dog22Grade 3
Bird5542Grade 9

 

Want:

Var1Var2Var3
Grade 144Cat
Grade 322Dog
Grade 95542Bird

 

When I tried to rearrange the order of variables , SAS is importing/reading it incorrectly - here is the output from SAS - Its putting the rows under different headers like this - Notice Var1 and Var3 :

Issue:

Var1Var2Var3
Cat44Grade 1
Dog22Grade 3
Bird5542Grade 9

 

How do I fix this without sorting the vars in the csv file

Thank you

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Just DEFINE the variables in a different order than you INPUT them.

 

data want ;
  infile 'myfile.csv' dsd truncover firstobs=2;
  length var1 $20 var2 8 Var3 $8 ;
  input var3 var2 var1;
run;

View solution in original post

5 REPLIES 5
AZIQ1
Quartz | Level 8

Here is the code:

DATA &FILENEW_I;

INFILE "filepath\Test.CSV" DSD FIRSTOBS=2;

 

Length Var1   $6     ;

Length Var2   $8     ;

Length Var3   $6     ;

INPUT

Var1   $

Var2 $

Var3 $;

Run;

Tom
Super User Tom
Super User

Just DEFINE the variables in a different order than you INPUT them.

 

data want ;
  infile 'myfile.csv' dsd truncover firstobs=2;
  length var1 $20 var2 8 Var3 $8 ;
  input var3 var2 var1;
run;
AZIQ1
Quartz | Level 8

Thank you,

Thats what I did, but its reading Var3 data in Var1 and Var1 data in Var3:

Here is my code

DATA &FILENEW_I;

INFILE "filepath\Test.CSV" DSD FIRSTOBS=2;

 Length Var1   $6     ;    Length Var2   $8     ;  Length Var3   $6     ;

INPUT  Var1   $   Var2 $  Var3 $;

Run;

novinosrin
Tourmaline | Level 20

just follow the pattern for the compiler to get the order right

 

data have;
informat var1 $10. var2 8. var3 $10.;
input Var3	Var2	Var1 &;
cards;
Cat	44	Grade 1
Dog	22	Grade 3
Bird	5542	Grade 9
;

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 5 replies
  • 2019 views
  • 0 likes
  • 4 in conversation