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

 

I am importing a csv file. After dropping the first lines with the metadata I am trying to transform the two columns into date and numeric formats respectively:

 

proc import datafile="/path/RealGDP2001-20016_Bulgaria.csv"
out=GDP_Bulgaria dbms=csv replace; 
getnames=no; 
run;

data GDP_Bulgaria; set GDP_Bulgaria; 
	if _n_>5; 
run;
                                                             

data GDP_Bulgaria; set GDP_Bulgaria; 
	date = input(var1, YYQ6.);
	format date yyq.;
	GDP = input(var2,8.);
	drop var1;
run;

No problems with the date, however for the numeric variable nothing seems to work. I tried compress(), trim(), strip() and vvalue() transformations before attempting to convert without success.

 

The problem seems to be related with the fact that some values have minus and other dont. If I change the variable length to 4 then those with minus are converted but the others aren't regardless if I am using compress or not.

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Don't use PROC IMPORT to read a CSV file with only two columns. It will take less code to write the DATA step, plus you get to have complete control over the variable names and types.

 

data want ;
  infile cards dsd firstobs=6 truncover;
  length Date GDP 8 ;
  informat date yyq. ;
  format date yyq6. ;
  input Date GDP ;
cards4;
Data Source in SDW: http://sdw.ecb.europa.eu/quickview.do?SERIES_KEY=320.MNA.Q.Y.BG.W2.S1.S1.B.B1GQ._Z._Z._Z.XDC.LR.GY
,MNA.Q.Y.BG.W2.S1.S1.B.B1GQ._Z._Z._Z.XDC.LR.GY
",""Gross domestic product at market prices - Bulgaria - Domestic (home or reference area), Total economy, Domestic currency (incl. conversion to current currency made using a fix parity), Chain linked volume (rebased), Growth rate, over 1 year, Calendar a
Collection:,Summed through period (S)
Period\Unit:,
2016Q3,3.4
2016Q2,3.6
2016Q1,3.4
2015Q4,3.7
;;;;

View solution in original post

5 REPLIES 5
Shmuel
Garnet | Level 18

Change GDP informat:

  

GDP = input(var2,best8.1);  

 

KonstantinV
Fluorite | Level 6

I just tried but it doesn't work.

Astounding
PROC Star

Your results are consistent with VAR2 being numeric originally.  All you would need to do is rename it to GDP.

art297
Opal | Level 21

Not sure what your problem is since your code does work for me. However, that said, here is a work around the should work:

 

proc import datafile="c:\art\RealGDP2001-20016_Bulgaria.csv"
out=GDP_Bulgaria dbms=csv replace; 
getnames=no;
datarow=6;
run;

data GDP_Bulgaria;
  set GDP_Bulgaria (rename=(var2=GDP));
  date = input(var1, YYQ6.);
  format date yyq.;
  drop var1;
run;

HTH,

Art, CEO, AnalystFinder.com

 

Tom
Super User Tom
Super User

Don't use PROC IMPORT to read a CSV file with only two columns. It will take less code to write the DATA step, plus you get to have complete control over the variable names and types.

 

data want ;
  infile cards dsd firstobs=6 truncover;
  length Date GDP 8 ;
  informat date yyq. ;
  format date yyq6. ;
  input Date GDP ;
cards4;
Data Source in SDW: http://sdw.ecb.europa.eu/quickview.do?SERIES_KEY=320.MNA.Q.Y.BG.W2.S1.S1.B.B1GQ._Z._Z._Z.XDC.LR.GY
,MNA.Q.Y.BG.W2.S1.S1.B.B1GQ._Z._Z._Z.XDC.LR.GY
",""Gross domestic product at market prices - Bulgaria - Domestic (home or reference area), Total economy, Domestic currency (incl. conversion to current currency made using a fix parity), Chain linked volume (rebased), Growth rate, over 1 year, Calendar a
Collection:,Summed through period (S)
Period\Unit:,
2016Q3,3.4
2016Q2,3.6
2016Q1,3.4
2015Q4,3.7
;;;;

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
  • 5 replies
  • 973 views
  • 1 like
  • 5 in conversation