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

Hello,

how to import the csv data in to sas that having variables' name and variables w1, w2 and w3 be under body_weight_different_time and variables L1, L2 and L3 be under  body_length_different_time. Appreciate you helping me solve the issue. Bellow are my code and data set.

 

options VALIDVARNAME=V7;
proc import datafile="path/mydata.csv" 

out= myoutput
dbms=csv   

replace;
guessingrows=max;
getnames=yes;
datarow=1;
run;
proc print data=myoutput;
run;

1 ACCEPTED SOLUTION

Accepted Solutions
fatemeh
Quartz | Level 8

Thanks for your response. I changes the datarow to 2 but still the issue is not solved. w2 and w3  are not under body_weight_different_time and also the same issue for L2 and L3 that they are not under body_length_different_time. I attached the CSV data and the result in pdf . Appreciate for any help.

fatemeh_2-1608441036182.png

options VALIDVARNAME=V7;
proc import datafile="mypath/Book1.csv"

out= myoutput
dbms=csv

replace;
guessingrows=max;
getnames=yes;
datarow=2;
run;
proc print data=myoutput;
run;

 

 

View solution in original post

3 REPLIES 3
fatemeh
Quartz | Level 8

Thanks for your response. I changes the datarow to 2 but still the issue is not solved. w2 and w3  are not under body_weight_different_time and also the same issue for L2 and L3 that they are not under body_length_different_time. I attached the CSV data and the result in pdf . Appreciate for any help.

fatemeh_2-1608441036182.png

options VALIDVARNAME=V7;
proc import datafile="mypath/Book1.csv"

out= myoutput
dbms=csv

replace;
guessingrows=max;
getnames=yes;
datarow=2;
run;
proc print data=myoutput;
run;

 

 

Tom
Super User Tom
Super User

What is the question?  It sounds like you have a non-rectangular structure and what to convert that into a SAS dataset.  What do you want to do with the extra information that is in the first row? Please show what you want the SAS dataset to look like for that source data.  

 

Note to post text just use the Insert Code button (looks like < / >). No need to attach a CSV file, especially since the preview button in this forum will not show the text of the CSV file, instead it tries to read it into a spreadsheet.  Here is what was in your CSV file:

,,,,,,body_weight_different_time ,,,,body_length_different_time,,
,var1,var2,var3,var4,var5,w1,w2,w3,s4,L1,L2,L3
1,123,KJJ,BC,HJ,12,15,18,20,k,123,125,158
2,1548,KJH,BD,HF,9,14,18,19,d,136,138,139
3,2546,KJU,BH,HD,8,13,15,18,c,201,205,206

It is not clear how the first row relates to the rest of the file.

 

If you have a CSV file then skip the "import" step and instead just READ the file directly with a data step.

Then you could incorporate any extra information you have about the structure into the logic of your data step.  For example if the first row is supposed to indicate that there are repeating groups of variable then use a DO loop to generate a separate output observation for each group.

data want;
  infile cards dsd firstobs=3 truncover;
  input id @ ;
  do year=1 to 2;
    input height weight @;
    output;
  end;
cards;
,YEAR1,,YEAR2,
ID,HEIGHT,WEIGHT,HEIGHT,WEIGHT
1,48,70,52,80
2,36,55,39,60
;

Results:

Obs    id    year    height    weight

 1      1      1       48        70
 2      1      2       52        80
 3      2      1       36        55
 4      2      2       39        60

 

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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