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:
Var3 | Var2 | Var1 |
Cat | 44 | Grade 1 |
Dog | 22 | Grade 3 |
Bird | 5542 | Grade 9 |
Want:
Var1 | Var2 | Var3 |
Grade 1 | 44 | Cat |
Grade 3 | 22 | Dog |
Grade 9 | 5542 | Bird |
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:
Var1 | Var2 | Var3 |
Cat | 44 | Grade 1 |
Dog | 22 | Grade 3 |
Bird | 5542 | Grade 9 |
How do I fix this without sorting the vars in the csv file
Thank you
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;
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;
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;
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;
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
;
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!
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.